[TASK] Ensure that cli user is admin on extension install and uninstall
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / CommandControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * All rights reserved
8 *
9 * This class is a backport of the corresponding class of TYPO3 Flow.
10 * All credits go to the TYPO3 Flow team.
11 *
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Test case
31 */
32 class CommandControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Mvc\Controller\CommandController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
36 */
37 protected $commandController;
38
39 public function setUp() {
40 $this->commandController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\CommandController', array('dummyCommand'));
41 }
42
43 /**
44 * @test
45 */
46 public function outputAppendsGivenStringToTheResponseContent() {
47 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
48 $mockResponse->expects($this->once())->method('appendContent')->with('some text');
49 $this->commandController->_set('response', $mockResponse);
50 $this->commandController->_call('output', 'some text');
51 }
52
53 /**
54 * @test
55 */
56 public function outputReplacesArgumentsInGivenString() {
57 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
58 $mockResponse->expects($this->once())->method('appendContent')->with('some text');
59 $this->commandController->_set('response', $mockResponse);
60 $this->commandController->_call('output', '%2$s %1$s', array('text', 'some'));
61 }
62
63 /**
64 * @test
65 */
66 public function outputLineAppendsGivenStringAndNewlineToTheResponseContent() {
67 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
68 $mockResponse->expects($this->once())->method('appendContent')->with('some text' . PHP_EOL);
69 $this->commandController->_set('response', $mockResponse);
70 $this->commandController->_call('outputLine', 'some text');
71 }
72
73 /**
74 * @test
75 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
76 */
77 public function quitThrowsStopActionException() {
78 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
79 $this->commandController->_set('response', $mockResponse);
80 $this->commandController->_call('quit');
81 }
82
83 /**
84 * @test
85 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
86 */
87 public function quitSetsResponseExitCode() {
88 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Cli\\Response');
89 $mockResponse->expects($this->once())->method('setExitCode')->with(123);
90 $this->commandController->_set('response', $mockResponse);
91 $this->commandController->_call('quit', 123);
92 }
93
94 /**
95 * @test
96 */
97 public function settingRequestAdminPropertySetsAdminRoleInUserAuthentication() {
98 $mockedUserAuthentication = $this->getMock('TYPO3\\CMS\\Core\\Authentication\\AbstractUserAuthentication');
99 $mockedUserAuthentication->user['admin'] = 42;
100 $this->commandController->expects($this->once())
101 ->method('dummyCommand')
102 ->will(
103 $this->returnCallback(
104 function() use ($mockedUserAuthentication) {
105 if ($mockedUserAuthentication->user['admin'] !== 1) {
106 throw new \Exception('User role is not admin');
107 }
108 }
109 ));
110 $this->commandController->_set('userAuthentication', $mockedUserAuthentication);
111 $this->commandController->_set('arguments', array());
112 $this->commandController->_set('commandMethodName', 'dummyCommand');
113 $this->commandController->_set('requestAdminPermissions', TRUE);
114 $this->commandController->_call('callCommandMethod');
115
116 $this->assertSame(42, $mockedUserAuthentication->user['admin']);
117 }
118 }