/tests/Unit/Libraries/Cms/MVC/Controller/BaseControllerTest.php

https://github.com/joomla/joomla-cms · PHP · 993 lines · 633 code · 105 blank · 255 comment · 0 complexity · 5ba4fb06e1cfe021674fb4a2bd1dc192 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Base
  5. *
  6. * @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. namespace Joomla\Tests\Unit\Libraries\Cms\MVC\Controller;
  10. use Exception;
  11. use Joomla\CMS\Application\CMSApplication;
  12. use Joomla\CMS\Document\Document;
  13. use Joomla\CMS\MVC\Controller\BaseController;
  14. use Joomla\CMS\MVC\Factory\LegacyFactory;
  15. use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  16. use Joomla\CMS\MVC\Model\BaseModel;
  17. use Joomla\CMS\MVC\View\AbstractView;
  18. use Joomla\CMS\User\CurrentUserInterface;
  19. use Joomla\CMS\User\CurrentUserTrait;
  20. use Joomla\CMS\User\User;
  21. use Joomla\Input\Input;
  22. use Joomla\Tests\Unit\UnitTestCase;
  23. /**
  24. * Test class for \Joomla\CMS\MVC\Controller\BaseController
  25. *
  26. * @package Joomla.UnitTest
  27. * @subpackage MVC
  28. *
  29. * @testdox The BaseController
  30. *
  31. * @since 4.2.0
  32. */
  33. class BaseControllerTest extends UnitTestCase
  34. {
  35. /**
  36. * @testdox contains the right dependencies
  37. *
  38. * @return void
  39. *
  40. * @since 4.2.0
  41. */
  42. public function testInjectedDependencies()
  43. {
  44. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  45. $app = $this->createStub(CMSApplication::class);
  46. $input = new Input();
  47. $controller = new class (['base_path' => __DIR__], $mvcFactory, $app, $input) extends BaseController
  48. {
  49. public function getMVCFactory(): MVCFactoryInterface
  50. {
  51. return $this->factory;
  52. }
  53. public function getApplication(): CMSApplication
  54. {
  55. return $this->app;
  56. }
  57. public function getInput(): Input
  58. {
  59. return $this->input;
  60. }
  61. };
  62. $this->assertEquals($mvcFactory, $controller->getMVCFactory());
  63. $this->assertEquals($input, $controller->getInput());
  64. }
  65. /**
  66. * @testdox gets the injected name
  67. *
  68. * @return void
  69. *
  70. * @since 4.2.0
  71. */
  72. public function testGetInjectedName()
  73. {
  74. $controller = new class (
  75. ['name' => 'unit test', 'base_path' => __DIR__],
  76. $this->createStub(MVCFactoryInterface::class),
  77. $this->createStub(CMSApplication::class)
  78. ) extends BaseController
  79. {
  80. };
  81. $this->assertEquals('unit test', $controller->getName());
  82. }
  83. /**
  84. * @testdox compiles its own name
  85. *
  86. * @return void
  87. *
  88. * @since 4.2.0
  89. */
  90. public function testGetCompiledName()
  91. {
  92. $controller = new class (
  93. ['base_path' => __DIR__],
  94. $this->createStub(MVCFactoryInterface::class),
  95. $this->createStub(CMSApplication::class)
  96. ) extends BaseController
  97. {
  98. };
  99. $this->assertStringContainsStringIgnoringCase('base', $controller->getName());
  100. }
  101. /**
  102. * @testdox lists the correct tasks
  103. *
  104. * @return void
  105. *
  106. * @since 4.2.0
  107. */
  108. public function testAvailableTasks()
  109. {
  110. $controller = new class (
  111. ['base_path' => __DIR__],
  112. $this->createStub(MVCFactoryInterface::class),
  113. $this->createStub(CMSApplication::class)
  114. ) extends BaseController
  115. {
  116. public function unit()
  117. {
  118. }
  119. };
  120. $this->assertEquals(['unit', 'display'], $controller->getTasks());
  121. }
  122. /**
  123. * @testdox can unregister a task
  124. *
  125. * @return void
  126. *
  127. * @since 4.2.0
  128. */
  129. public function testUnregisterTask()
  130. {
  131. $controller = new class (
  132. ['base_path' => __DIR__],
  133. $this->createStub(MVCFactoryInterface::class),
  134. $this->createStub(CMSApplication::class)
  135. ) extends BaseController
  136. {
  137. public function list()
  138. {
  139. return $this->taskMap;
  140. }
  141. };
  142. $controller->unregisterTask('list');
  143. $this->assertNotContains('list', $controller->list());
  144. }
  145. /**
  146. * @testdox executes a task
  147. *
  148. * @return void
  149. *
  150. * @since 4.2.0
  151. */
  152. public function testExecuteTask()
  153. {
  154. $controller = new class (
  155. ['base_path' => __DIR__],
  156. $this->createStub(MVCFactoryInterface::class),
  157. $this->createStub(CMSApplication::class)
  158. ) extends BaseController
  159. {
  160. public function unit()
  161. {
  162. return 'unit test';
  163. }
  164. };
  165. $this->assertEquals('unit test', $controller->execute('unit'));
  166. $this->assertEquals('unit', $controller->getTask());
  167. }
  168. /**
  169. * @testdox can execute the injected default task
  170. *
  171. * @return void
  172. *
  173. * @since 4.2.0
  174. */
  175. public function testExecuteInjectedDefaultTask()
  176. {
  177. $controller = new class (
  178. ['default_task' => 'unit', 'base_path' => __DIR__],
  179. $this->createStub(MVCFactoryInterface::class),
  180. $this->createStub(CMSApplication::class)
  181. ) extends BaseController
  182. {
  183. public function unit()
  184. {
  185. return 'unit test';
  186. }
  187. };
  188. $this->assertEquals('unit test', $controller->execute('unit'));
  189. $this->assertEquals('unit', $controller->getTask());
  190. }
  191. /**
  192. * @testdox executes the display default task
  193. *
  194. * @return void
  195. *
  196. * @since 4.2.0
  197. */
  198. public function testExecuteDisplayDefaultTask()
  199. {
  200. $controller = new class (
  201. ['base_path' => __DIR__],
  202. $this->createStub(MVCFactoryInterface::class),
  203. $this->createStub(CMSApplication::class)
  204. ) extends BaseController
  205. {
  206. public function display($cachable = false, $urlparams = array())
  207. {
  208. return 'unit test';
  209. }
  210. };
  211. $this->assertEquals('unit test', $controller->execute(''));
  212. $this->assertEquals('', $controller->getTask());
  213. }
  214. /**
  215. * @testdox throws an exception when a task doesn't exist
  216. *
  217. * @return void
  218. *
  219. * @since 4.2.0
  220. */
  221. public function testExecuteTaskWhichDoesntExist()
  222. {
  223. $controller = new class (
  224. ['default_task' => 'invalid', 'base_path' => __DIR__],
  225. $this->createStub(MVCFactoryInterface::class),
  226. $this->createStub(CMSApplication::class)
  227. ) extends BaseController
  228. {
  229. };
  230. $this->expectException(Exception::class);
  231. $controller->execute('unit');
  232. }
  233. /**
  234. * @testdox returns the correct model
  235. *
  236. * @return void
  237. *
  238. * @since 4.2.0
  239. */
  240. public function testGetModel()
  241. {
  242. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  243. $model = new class (['name' => 'test']) extends BaseModel
  244. {
  245. };
  246. $mvcFactory->method('createModel')->willReturn($model);
  247. $controller = new class (
  248. ['base_path' => __DIR__],
  249. $mvcFactory,
  250. $this->createStub(CMSApplication::class),
  251. new Input()
  252. ) extends BaseController
  253. {
  254. };
  255. $this->assertEquals($model, $controller->getModel());
  256. }
  257. /**
  258. * @testdox returns the correct model with an injected prefix
  259. *
  260. * @return void
  261. *
  262. * @since 4.2.0
  263. */
  264. public function testGetModelWithInjectedPrefix()
  265. {
  266. $mvcFactory = $this->createMock(LegacyFactory::class);
  267. $mvcFactory->expects($this->once())->method('createModel')->with($this->equalTo('Unit'), $this->equalTo('Test'));
  268. $controller = new class (
  269. ['model_prefix' => 'Test', 'base_path' => __DIR__],
  270. $mvcFactory,
  271. $this->createStub(CMSApplication::class),
  272. new Input()
  273. ) extends BaseController
  274. {
  275. };
  276. $controller->getModel('Unit');
  277. }
  278. /**
  279. * @testdox returns the correct model with the app name prefix
  280. *
  281. * @return void
  282. *
  283. * @since 4.2.0
  284. */
  285. public function testGetModelWithAppNamePrefix()
  286. {
  287. $mvcFactory = $this->createMock(MVCFactoryInterface::class);
  288. $mvcFactory->expects($this->once())->method('createModel')->with($this->equalTo('Unit'), $this->equalTo('Test'));
  289. $app = $this->createStub(CMSApplication::class);
  290. $app->method('getName')->willReturn('Test');
  291. $controller = new class (
  292. ['base_path' => __DIR__],
  293. $mvcFactory,
  294. $app,
  295. new Input()
  296. ) extends BaseController
  297. {
  298. };
  299. $controller->getModel('Unit');
  300. }
  301. /**
  302. * @testdox returns false when no model is available
  303. *
  304. * @return void
  305. *
  306. * @since 4.2.0
  307. */
  308. public function testGetNullModel()
  309. {
  310. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  311. $controller = new class (
  312. ['base_path' => __DIR__],
  313. $mvcFactory,
  314. $this->createStub(CMSApplication::class),
  315. new Input()
  316. ) extends BaseController
  317. {
  318. };
  319. $this->assertFalse($controller->getModel());
  320. }
  321. /**
  322. * @testdox returns the correct model with the identity from the app
  323. *
  324. * @return void
  325. *
  326. * @since 4.2.0
  327. */
  328. public function testGetModelWithIdentity()
  329. {
  330. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  331. $model = new class (['name' => 'test']) extends BaseModel implements CurrentUserInterface
  332. {
  333. use CurrentUserTrait;
  334. public function getUser(): User
  335. {
  336. return $this->getCurrentUser();
  337. }
  338. };
  339. $mvcFactory->method('createModel')->willReturn($model);
  340. $user = new User();
  341. $app = $this->createStub(CMSApplication::class);
  342. $app->method('getIdentity')->willReturn($user);
  343. $controller = new class (
  344. ['base_path' => __DIR__],
  345. $mvcFactory,
  346. $app,
  347. new Input()
  348. ) extends BaseController
  349. {
  350. };
  351. $this->assertEquals($user, $controller->getModel()->getUser());
  352. }
  353. /**
  354. * @testdox returns the correct view
  355. *
  356. * @return void
  357. *
  358. * @since 4.2.0
  359. */
  360. public function testGetView()
  361. {
  362. $view = new class (['name' => 'test']) extends AbstractView
  363. {
  364. public function display($tpl = null)
  365. {
  366. }
  367. };
  368. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  369. $mvcFactory->method('createView')->willReturn($view);
  370. $controller = new class (
  371. ['base_path' => __DIR__],
  372. $mvcFactory,
  373. $this->createStub(CMSApplication::class),
  374. new Input()
  375. ) extends BaseController
  376. {
  377. };
  378. $this->assertEquals($view, $controller->getView('testGetView'));
  379. }
  380. /**
  381. * @testdox returns the correct view with an injected prefix
  382. *
  383. * @return void
  384. *
  385. * @since 4.2.0
  386. */
  387. public function testGetViewWithInjectedPrefix()
  388. {
  389. $mvcFactory = $this->createMock(LegacyFactory::class);
  390. $mvcFactory->expects($this->once())->method('createView')->with($this->equalTo('Unit'), $this->equalTo('TestView'))
  391. ->willReturn($this->createStub(AbstractView::class));
  392. $controller = new class (
  393. ['name' => 'Test', 'base_path' => __DIR__],
  394. $mvcFactory,
  395. $this->createStub(CMSApplication::class),
  396. new Input()
  397. ) extends BaseController
  398. {
  399. };
  400. $controller->getView('Unit');
  401. }
  402. /**
  403. * @testdox throws an exception when no view is available
  404. *
  405. * @return void
  406. *
  407. * @since 4.2.0
  408. */
  409. public function testGetNullView()
  410. {
  411. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  412. $controller = new class (
  413. ['base_path' => __DIR__],
  414. $mvcFactory,
  415. $this->createStub(CMSApplication::class),
  416. new Input()
  417. ) extends BaseController
  418. {
  419. };
  420. $this->expectException(Exception::class);
  421. $controller->getView('testGetNullView');
  422. }
  423. /**
  424. * @testdox returns the correct view with the identity from the app
  425. *
  426. * @return void
  427. *
  428. * @since 4.2.0
  429. */
  430. public function testGetViewWithIdentity()
  431. {
  432. $view = new class (['name' => 'test']) extends AbstractView implements CurrentUserInterface
  433. {
  434. use CurrentUserTrait;
  435. public function display($tpl = null)
  436. {
  437. }
  438. public function getUser(): User
  439. {
  440. return $this->getCurrentUser();
  441. }
  442. };
  443. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  444. $mvcFactory->method('createView')->willReturn($view);
  445. $user = new User();
  446. $app = $this->createStub(CMSApplication::class);
  447. $app->method('getIdentity')->willReturn($user);
  448. $controller = new class (
  449. ['base_path' => __DIR__],
  450. $mvcFactory,
  451. $app,
  452. new Input()
  453. ) extends BaseController
  454. {
  455. };
  456. $this->assertEquals($user, $controller->getView('testGetViewWithIdentity')->getUser());
  457. }
  458. /**
  459. * @testdox gets the injected view path
  460. *
  461. * @return void
  462. *
  463. * @since 4.2.0
  464. */
  465. public function testInjectViewPath()
  466. {
  467. $path = dirname(__DIR__);
  468. $controller = new class (
  469. ['view_path' => $path, 'base_path' => __DIR__],
  470. $this->createStub(MVCFactoryInterface::class),
  471. $this->createStub(CMSApplication::class)
  472. ) extends BaseController
  473. {
  474. public function getPaths()
  475. {
  476. return $this->paths;
  477. }
  478. };
  479. $this->arrayHasKey('view', $controller->getPaths());
  480. $this->assertEquals([$path . '/'], $controller->getPaths()['view']);
  481. }
  482. /**
  483. * @testdox can add a view path
  484. *
  485. * @return void
  486. *
  487. * @since 4.2.0
  488. */
  489. public function testAddViewPath()
  490. {
  491. $controller = new class (
  492. ['name' => 'unit test', 'base_path' => __DIR__],
  493. $this->createStub(MVCFactoryInterface::class),
  494. $this->createStub(CMSApplication::class)
  495. ) extends BaseController
  496. {
  497. public function getPaths()
  498. {
  499. return $this->paths;
  500. }
  501. };
  502. $controller->addViewPath(__DIR__);
  503. $this->arrayHasKey('view', $controller->getPaths());
  504. $this->assertContains(__DIR__ . '/', $controller->getPaths()['view']);
  505. }
  506. /**
  507. * @testdox can display
  508. *
  509. * @return void
  510. *
  511. * @since 4.2.0
  512. */
  513. public function testDisplay()
  514. {
  515. $app = $this->createStub(CMSApplication::class);
  516. $app->method('getDocument')->willReturn(new Document());
  517. $view = new class (['name' => 'test']) extends AbstractView
  518. {
  519. public $value = null;
  520. public function display($tpl = null)
  521. {
  522. $this->value = 'unit test';
  523. }
  524. };
  525. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  526. $mvcFactory->method('createView')->willReturn($view);
  527. $controller = new class (
  528. ['default_view' => 'unit', 'base_path' => __DIR__],
  529. $mvcFactory,
  530. $app,
  531. new Input()
  532. ) extends BaseController
  533. {
  534. };
  535. $controller->display(false);
  536. $this->assertEquals('unit test', $view->value);
  537. }
  538. /**
  539. * @testdox can display and sets a model on the view
  540. *
  541. * @return void
  542. *
  543. * @since 4.2.0
  544. */
  545. public function testDisplayWithModel()
  546. {
  547. $app = $this->createStub(CMSApplication::class);
  548. $app->method('getDocument')->willReturn(new Document());
  549. $model = new class (['name' => 'test']) extends BaseModel
  550. {
  551. };
  552. $view = new class (['name' => 'test']) extends AbstractView
  553. {
  554. public function display($tpl = null)
  555. {
  556. }
  557. };
  558. $mvcFactory = $this->createStub(MVCFactoryInterface::class);
  559. $mvcFactory->method('createView')->willReturn($view);
  560. $mvcFactory->method('createModel')->willReturn($model);
  561. $controller = new class (
  562. ['default_view' => 'modeltest', 'base_path' => __DIR__],
  563. $mvcFactory,
  564. $app,
  565. new Input()
  566. ) extends BaseController
  567. {
  568. };
  569. $controller->display(false);
  570. $this->assertEquals($model, $view->getModel());
  571. }
  572. /**
  573. * @testdox can check the edit id when it exists in user state
  574. *
  575. * @return void
  576. *
  577. * @since 4.2.0
  578. */
  579. public function testCheckEditIdExist()
  580. {
  581. $app = $this->createStub(CMSApplication::class);
  582. $app->method('getUserState')->willReturn([1]);
  583. $controller = new class (
  584. ['base_path' => __DIR__],
  585. $this->createStub(MVCFactoryInterface::class),
  586. $app,
  587. new Input()
  588. ) extends BaseController
  589. {
  590. public function checkEditId($context, $id)
  591. {
  592. return parent::checkEditId($context, $id);
  593. }
  594. };
  595. $this->assertTrue($controller->checkEditId('', 1));
  596. }
  597. /**
  598. * @testdox cannot check the edit id when it doesn't exists in user state
  599. *
  600. * @return void
  601. *
  602. * @since 4.2.0
  603. */
  604. public function testCheckEditIdNotExist()
  605. {
  606. $app = $this->createStub(CMSApplication::class);
  607. $app->method('getUserState')->willReturn([1]);
  608. $controller = new class (
  609. ['base_path' => __DIR__],
  610. $this->createStub(MVCFactoryInterface::class),
  611. $app,
  612. new Input()
  613. ) extends BaseController
  614. {
  615. public function checkEditId($context, $id)
  616. {
  617. return parent::checkEditId($context, $id);
  618. }
  619. };
  620. $this->assertFalse($controller->checkEditId('', 2));
  621. }
  622. /**
  623. * @testdox cannot check the edit id when it is empty
  624. *
  625. * @return void
  626. *
  627. * @since 4.2.0
  628. */
  629. public function testCheckEditEmptyId()
  630. {
  631. $app = $this->createStub(CMSApplication::class);
  632. $app->method('getUserState')->willReturn([1]);
  633. $controller = new class (
  634. ['base_path' => __DIR__],
  635. $this->createStub(MVCFactoryInterface::class),
  636. $app,
  637. new Input()
  638. ) extends BaseController
  639. {
  640. public function checkEditId($context, $id)
  641. {
  642. return parent::checkEditId($context, $id);
  643. }
  644. };
  645. $this->assertTrue($controller->checkEditId('', 0));
  646. }
  647. /**
  648. * @testdox can hold the edit id in app user state
  649. *
  650. * @return void
  651. *
  652. * @since 4.2.0
  653. */
  654. public function testHoldEditId()
  655. {
  656. $app = $this->createMock(CMSApplication::class);
  657. $app->expects($this->once())->method('setUserState')->with($this->equalTo('unit.id'), $this->equalTo([1]));
  658. $controller = new class (
  659. ['base_path' => __DIR__],
  660. $this->createStub(MVCFactoryInterface::class),
  661. $app,
  662. new Input()
  663. ) extends BaseController
  664. {
  665. public function holdEditId($context, $id)
  666. {
  667. return parent::holdEditId($context, $id);
  668. }
  669. };
  670. $controller->holdEditId('unit', 1);
  671. }
  672. /**
  673. * @testdox cannot hold an empty edit id in app user state
  674. *
  675. * @return void
  676. *
  677. * @since 4.2.0
  678. */
  679. public function testHoldEditEmptyId()
  680. {
  681. $app = $this->createMock(CMSApplication::class);
  682. $app->expects($this->never())->method('setUserState');
  683. $controller = new class (
  684. ['base_path' => __DIR__],
  685. $this->createStub(MVCFactoryInterface::class),
  686. $app,
  687. new Input()
  688. ) extends BaseController
  689. {
  690. public function holdEditId($context, $id)
  691. {
  692. return parent::holdEditId($context, $id);
  693. }
  694. };
  695. $controller->holdEditId('unit', 0);
  696. }
  697. /**
  698. * @testdox can release the edit id from app user state
  699. *
  700. * @return void
  701. *
  702. * @since 4.2.0
  703. */
  704. public function testReleaseEditId()
  705. {
  706. $app = $this->createMock(CMSApplication::class);
  707. $app->method('getUserState')->willReturn([1, 2]);
  708. $app->expects($this->once())->method('setUserState')->with($this->equalTo('unit.id'), $this->equalTo([1 => 2]));
  709. $controller = new class (
  710. ['base_path' => __DIR__],
  711. $this->createStub(MVCFactoryInterface::class),
  712. $app,
  713. new Input()
  714. ) extends BaseController
  715. {
  716. public function releaseEditId($context, $id)
  717. {
  718. return parent::releaseEditId($context, $id);
  719. }
  720. };
  721. $controller->releaseEditId('unit', 1);
  722. }
  723. /**
  724. * @testdox cannot release the edit id from app user state when it doesn't exist
  725. *
  726. * @return void
  727. *
  728. * @since 4.2.0
  729. */
  730. public function testReleaseInvalidEditId()
  731. {
  732. $app = $this->createMock(CMSApplication::class);
  733. $app->method('getUserState')->willReturn([2]);
  734. $app->expects($this->never())->method('setUserState');
  735. $controller = new class (
  736. ['base_path' => __DIR__],
  737. $this->createStub(MVCFactoryInterface::class),
  738. $app,
  739. new Input()
  740. ) extends BaseController
  741. {
  742. public function releaseEditId($context, $id)
  743. {
  744. return parent::releaseEditId($context, $id);
  745. }
  746. };
  747. $controller->releaseEditId('unit', 1);
  748. }
  749. /**
  750. * @testdox sets the correct redirect, message and type
  751. *
  752. * @return void
  753. *
  754. * @since 4.2.0
  755. */
  756. public function testSetRedirect()
  757. {
  758. $controller = new class (
  759. ['base_path' => __DIR__],
  760. $this->createStub(MVCFactoryInterface::class),
  761. $this->createStub(CMSApplication::class),
  762. new Input()
  763. ) extends BaseController
  764. {
  765. public function getRedirect()
  766. {
  767. return $this->redirect;
  768. }
  769. public function getMessage()
  770. {
  771. return $this->message;
  772. }
  773. public function getMessageType()
  774. {
  775. return $this->messageType;
  776. }
  777. };
  778. $controller->setRedirect('unit/test', 'unit', 'test');
  779. $this->assertEquals('unit/test', $controller->getRedirect());
  780. $this->assertEquals('unit', $controller->getMessage());
  781. $this->assertEquals('test', $controller->getMessageType());
  782. }
  783. /**
  784. * @testdox sets the correct redirect and has the default type 'message'
  785. *
  786. * @return void
  787. *
  788. * @since 4.2.0
  789. */
  790. public function testSetRedirectWithEmptyType()
  791. {
  792. $controller = new class (
  793. ['base_path' => __DIR__],
  794. $this->createStub(MVCFactoryInterface::class),
  795. $this->createStub(CMSApplication::class),
  796. new Input()
  797. ) extends BaseController
  798. {
  799. public function getMessageType()
  800. {
  801. return $this->messageType;
  802. }
  803. };
  804. $controller->setRedirect('unit/test');
  805. $this->assertEquals('message', $controller->getMessageType());
  806. }
  807. /**
  808. * @testdox does a redirect on the internal app instance
  809. *
  810. * @return void
  811. *
  812. * @since 4.2.0
  813. */
  814. public function testRedirect()
  815. {
  816. $app = $this->createMock(CMSApplication::class);
  817. $app->expects($this->once())->method('redirect')->with($this->equalTo('unit/test'));
  818. $app->expects($this->once())->method('enqueueMessage')->with($this->equalTo('unit test'));
  819. $controller = new class (
  820. ['base_path' => __DIR__],
  821. $this->createStub(MVCFactoryInterface::class),
  822. $app,
  823. new Input()
  824. ) extends BaseController
  825. {
  826. public function redirect()
  827. {
  828. $this->redirect = 'unit/test';
  829. $this->message = 'unit test';
  830. return parent::redirect();
  831. }
  832. };
  833. $this->assertFalse($controller->redirect());
  834. }
  835. /**
  836. * @testdox sets the correct message and type
  837. *
  838. * @return void
  839. *
  840. * @since 4.2.0
  841. */
  842. public function testSetMessage()
  843. {
  844. $controller = new class (
  845. ['base_path' => __DIR__],
  846. $this->createStub(MVCFactoryInterface::class),
  847. $this->createStub(CMSApplication::class),
  848. new Input()
  849. ) extends BaseController
  850. {
  851. public function getMessage()
  852. {
  853. return $this->message;
  854. }
  855. public function getMessageType()
  856. {
  857. return $this->messageType;
  858. }
  859. };
  860. $this->assertEmpty($controller->setMessage('unit', 'test'));
  861. $this->assertEquals('unit', $controller->getMessage());
  862. $this->assertEquals('test', $controller->getMessageType());
  863. }
  864. /**
  865. * @testdox returns the old message when a second time set
  866. *
  867. * @return void
  868. *
  869. * @since 4.2.0
  870. */
  871. public function testSetMessageTwice()
  872. {
  873. $controller = new class (
  874. ['base_path' => __DIR__],
  875. $this->createStub(MVCFactoryInterface::class),
  876. $this->createStub(CMSApplication::class),
  877. new Input()
  878. ) extends BaseController
  879. {
  880. public function getMessage()
  881. {
  882. return $this->message;
  883. }
  884. };
  885. $controller->setMessage('unit');
  886. $this->assertEquals('unit', $controller->setMessage('test'));
  887. }
  888. }