Merge pull request #14143 from nextcloud/bugfix/noid/add-link-to-settings-on-notification

Add a link to the notification to create the backup codes
This commit is contained in:
Roeland Jago Douma 2019-02-11 19:19:05 +01:00 committed by GitHub
commit e3c787682d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OCA\TwoFactorBackupCodes\Notifications; namespace OCA\TwoFactorBackupCodes\Notifications;
use OCP\IURLGenerator;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
use OCP\Notification\INotification; use OCP\Notification\INotification;
use OCP\Notification\INotifier; use OCP\Notification\INotifier;
@ -33,8 +34,12 @@ class Notifier implements INotifier {
/** @var IFactory */ /** @var IFactory */
private $factory; private $factory;
public function __construct(IFactory $factory) { /** @var IURLGenerator */
private $url;
public function __construct(IFactory $factory, IURLGenerator $url) {
$this->factory = $factory; $this->factory = $factory;
$this->url = $url;
} }
public function prepare(INotification $notification, $languageCode) { public function prepare(INotification $notification, $languageCode) {
@ -53,6 +58,8 @@ class Notifier implements INotifier {
)->setParsedMessage( )->setParsedMessage(
$l->t('You have enabled two-factor authentication but have not yet generated backup codes. Be sure to do this in case you lose access to your second factor.') $l->t('You have enabled two-factor authentication but have not yet generated backup codes. Be sure to do this in case you lose access to your second factor.')
); );
$notification->setLink($this->url->linkToRouteAbsolute('settings.PersonalSettings.index', ['section' => 'security']));
return $notification; return $notification;
default: default:

View File

@ -26,17 +26,21 @@ namespace OCA\TwoFactorBackupCodes\Tests\Unit\Notification;
use OCA\TwoFactorBackupCodes\Notifications\Notifier; use OCA\TwoFactorBackupCodes\Notifications\Notifier;
use OCP\IL10N; use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
use OCP\Notification\INotification; use OCP\Notification\INotification;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
class NotifierTest extends TestCase { class NotifierTest extends TestCase {
/** @var Notifier */ /** @var Notifier */
protected $notifier; protected $notifier;
/** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */ /** @var IFactory|MockObject */
protected $factory; protected $factory;
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ /** @var IURLGenerator|MockObject */
protected $url;
/** @var IL10N|MockObject */
protected $l; protected $l;
protected function setUp() { protected function setUp() {
@ -49,12 +53,14 @@ class NotifierTest extends TestCase {
return vsprintf($string, $args); return vsprintf($string, $args);
}); });
$this->factory = $this->createMock(IFactory::class); $this->factory = $this->createMock(IFactory::class);
$this->url = $this->createMock(IURLGenerator::class);
$this->factory->expects($this->any()) $this->factory->expects($this->any())
->method('get') ->method('get')
->willReturn($this->l); ->willReturn($this->l);
$this->notifier = new Notifier( $this->notifier = new Notifier(
$this->factory $this->factory,
$this->url
); );
} }
@ -114,6 +120,15 @@ class NotifierTest extends TestCase {
->with('You have enabled two-factor authentication but have not yet generated backup codes. Be sure to do this in case you lose access to your second factor.') ->with('You have enabled two-factor authentication but have not yet generated backup codes. Be sure to do this in case you lose access to your second factor.')
->willReturnSelf(); ->willReturnSelf();
$this->url->expects($this->once())
->method('linkToRouteAbsolute')
->with('settings.PersonalSettings.index', ['section' => 'security'])
->willReturn('linkToRouteAbsolute');
$notification->expects($this->once())
->method('setLink')
->with('linkToRouteAbsolute')
->willReturnSelf();
$return = $this->notifier->prepare($notification, 'nl'); $return = $this->notifier->prepare($notification, 'nl');
$this->assertEquals($notification, $return); $this->assertEquals($notification, $return);
} }