From 2b861f2421dc4f792f4596125de07ac15ee7423b Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Thu, 25 Aug 2016 10:53:13 +0200 Subject: inject prefilled data --- lib/controller/pagecontroller.php | 16 +++++++--- templates/index.php | 4 +-- tests/controller/pagecontrollertest.php | 54 ++++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/lib/controller/pagecontroller.php b/lib/controller/pagecontroller.php index e7b6913e5..188b190db 100644 --- a/lib/controller/pagecontroller.php +++ b/lib/controller/pagecontroller.php @@ -33,6 +33,7 @@ use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\IUserSession; class PageController extends Controller { @@ -51,6 +52,9 @@ class PageController extends Controller { /** @var string */ private $currentUserId; + /** @var IUserSession */ + private $userSession; + /** * @param string $appName * @param IRequest $request @@ -62,13 +66,14 @@ class PageController extends Controller { */ public function __construct($appName, IRequest $request, IURLGenerator $urlGenerator, IConfig $config, AccountService $accountService, - AliasesService $aliasesService, $UserId) { + AliasesService $aliasesService, $UserId, IUserSession $userSession) { parent::__construct($appName, $request); $this->urlGenerator = $urlGenerator; $this->config = $config; $this->accountService = $accountService; $this->aliasesService = $aliasesService; $this->currentUserId = $UserId; + $this->userSession = $userSession; } /** @@ -83,15 +88,17 @@ class PageController extends Controller { $accountsJson = []; foreach ($mailAccounts as $mailAccount) { $conf = $mailAccount->getConfiguration(); - $conf['aliases'] = $this->aliasesService->findAll($conf['accountId'], - $this->currentUserId); + $conf['aliases'] = $this->aliasesService->findAll($conf['accountId'], $this->currentUserId); $accountsJson[] = $conf; } + $user = $this->userSession->getUser(); $response = new TemplateResponse($this->appName, 'index', [ 'debug' => $this->config->getSystemValue('debug', false), 'app-version' => $this->config->getAppValue('mail', 'installed_version'), 'accounts' => base64_encode(json_encode($accountsJson)), + 'prefill_displayName' => $user->getDisplayName(), + 'prefill_email' => $this->config->getUserValue($user->getUID(), 'settings', 'email', ''), ]); // set csp rules for ownCloud 8.1 @@ -122,8 +129,7 @@ class PageController extends Controller { } } - array_walk($params, - function (&$value, $key) { + array_walk($params, function (&$value, $key) { $value = "$key=" . urlencode($value); }); diff --git a/templates/index.php b/templates/index.php index aa186ac28..1336066d1 100644 --- a/templates/index.php +++ b/templates/index.php @@ -43,9 +43,9 @@ if ($_['debug']) { + style="display: none"> + style="display: none">
diff --git a/tests/controller/pagecontrollertest.php b/tests/controller/pagecontrollertest.php index 356041147..a7f6e2cc7 100644 --- a/tests/controller/pagecontrollertest.php +++ b/tests/controller/pagecontrollertest.php @@ -18,10 +18,11 @@ * along with this program. If not, see * */ +use OCA\Mail\Controller\PageController; +use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; use Test\TestCase; -use OCA\Mail\Controller\PageController; class PageControllerTest extends TestCase { @@ -34,24 +35,26 @@ class PageControllerTest extends TestCase { private $controller; private $accountService; private $aliasesService; + private $userSession; protected function setUp() { parent::setUp(); $this->appName = 'mail'; $this->userId = 'george'; - $this->request = $this->getMock('\OCP\IRequest'); - $this->urlGenerator = $this->getMock('OCP\IURLGenerator'); - $this->config = $this->getMock('OCP\IConfig'); + $this->request = $this->getMockBuilder('\OCP\IRequest')->getMock(); + $this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')->getMock(); + $this->config = $this->getMockBuilder('OCP\IConfig')->getMock(); $this->accountService = $this->getMockBuilder('OCA\Mail\Service\AccountService') ->disableOriginalConstructor() ->getMock(); $this->aliasesService = $this->getMockBuilder('\OCA\Mail\Service\AliasesService') ->disableOriginalConstructor() ->getMock(); + $this->userSession = $this->getMockBuilder('OCP\IUserSession')->getMock(); $this->controller = new PageController($this->appName, $this->request, $this->urlGenerator, $this->config, $this->accountService, - $this->aliasesService, $this->userId); + $this->aliasesService, $this->userId, $this->userSession); } public function testIndex() { @@ -62,25 +65,25 @@ class PageControllerTest extends TestCase { ->method('findByUserId') ->with($this->userId) ->will($this->returnValue([ - $account1, - $account2, - ])); + $account1, + $account2, + ])); $account1->expects($this->once()) ->method('getConfiguration') ->will($this->returnValue([ - 'accountId' => 1, - ])); + 'accountId' => 1, + ])); $account2->expects($this->once()) ->method('getConfiguration') ->will($this->returnValue([ - 'accountId' => 2, - ])); + 'accountId' => 2, + ])); $this->aliasesService->expects($this->exactly(2)) ->method('findAll') ->will($this->returnValueMap([ - [1, $this->userId, ['a11', 'a12']], - [2, $this->userId, ['a21', 'a22']], - ])); + [1, $this->userId, ['a11', 'a12']], + [2, $this->userId, ['a21', 'a22']], + ])); $accountsJson = [ [ 'accountId' => 1, @@ -98,6 +101,10 @@ class PageControllerTest extends TestCase { ], ]; + $user = $this->getMockBuilder('\OCP\IUser')->getMock(); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); $this->config->expects($this->once()) ->method('getSystemValue') ->with('debug', false) @@ -106,16 +113,27 @@ class PageControllerTest extends TestCase { ->method('getAppValue') ->with('mail', 'installed_version') ->will($this->returnValue('1.2.3')); + $user->expects($this->once()) + ->method('getDisplayName') + ->will($this->returnValue('Jane Doe')); + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('jane')); + $this->config->expects($this->once()) + ->method('getUserValue') + ->with($this->equalTo('jane'), $this->equalTo('settings'), $this->equalTo('email'), $this->equalTo('')) + ->will($this->returnValue('jane@doe.cz')); - $expected = new TemplateResponse($this->appName, 'index', - [ + $expected = new TemplateResponse($this->appName, 'index', [ 'debug' => true, 'app-version' => '1.2.3', 'accounts' => base64_encode(json_encode($accountsJson)), + 'prefill_displayName' => 'Jane Doe', + 'prefill_email' => 'jane@doe.cz', ]); // set csp rules for ownCloud 8.1 if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) { - $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); + $csp = new ContentSecurityPolicy(); $csp->addAllowedFrameDomain('\'self\''); $expected->setContentSecurityPolicy($csp); } -- cgit v1.2.3