| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\View\Helper;
- use Cake\Core\Plugin;
- use Cake\Http\ServerRequest;
- use Cake\Http\Session;
- use Cake\TestSuite\TestCase;
- use Cake\View\Helper\FlashHelper;
- use Cake\View\View;
- /**
- * FlashHelperTest class
- *
- * @property \Cake\View\Helper\FlashHelper $Flash
- */
- class FlashHelperTest extends TestCase
- {
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $session = new Session();
- $this->View = new View(new ServerRequest(['session' => $session]));
- $this->Flash = new FlashHelper($this->View);
- $session->write([
- 'Flash' => [
- 'flash' => [
- [
- 'key' => 'flash',
- 'message' => 'This is a calling',
- 'element' => 'Flash/default',
- 'params' => [],
- ],
- ],
- 'notification' => [
- [
- 'key' => 'notification',
- 'message' => 'This is a test of the emergency broadcasting system',
- 'element' => 'flash_helper',
- 'params' => [
- 'title' => 'Notice!',
- 'name' => 'Alert!',
- ],
- ],
- ],
- 'classy' => [
- [
- 'key' => 'classy',
- 'message' => 'Recorded',
- 'element' => 'flash_classy',
- 'params' => [],
- ],
- ],
- 'stack' => [
- [
- 'key' => 'flash',
- 'message' => 'This is a calling',
- 'element' => 'Flash/default',
- 'params' => [],
- ],
- [
- 'key' => 'notification',
- 'message' => 'This is a test of the emergency broadcasting system',
- 'element' => 'flash_helper',
- 'params' => [
- 'title' => 'Notice!',
- 'name' => 'Alert!',
- ],
- ],
- [
- 'key' => 'classy',
- 'message' => 'Recorded',
- 'element' => 'flash_classy',
- 'params' => [],
- ],
- ],
- ],
- ]);
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- unset($this->View, $this->Flash);
- $this->clearPlugins();
- }
- /**
- * testFlash method
- *
- * @return void
- */
- public function testFlash()
- {
- $result = $this->Flash->render();
- $expected = '<div class="message">This is a calling</div>';
- $this->assertContains($expected, $result);
- $expected = '<div id="classy-message">Recorded</div>';
- $result = $this->Flash->render('classy');
- $this->assertEquals($expected, $result);
- $result = $this->Flash->render('notification');
- $expected = [
- 'div' => ['id' => 'notificationLayout'],
- '<h1', 'Alert!', '/h1',
- '<h3', 'Notice!', '/h3',
- '<p', 'This is a test of the emergency broadcasting system', '/p',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->assertNull($this->Flash->render('non-existent'));
- }
- /**
- * testFlashThrowsException
- */
- public function testFlashThrowsException()
- {
- $this->expectException(\UnexpectedValueException::class);
- $this->View->getRequest()->getSession()->write('Flash.foo', 'bar');
- $this->Flash->render('foo');
- }
- /**
- * test setting the element from the attrs.
- *
- * @return void
- */
- public function testFlashElementInAttrs()
- {
- $result = $this->Flash->render('notification', [
- 'element' => 'flash_helper',
- 'params' => ['title' => 'Notice!', 'name' => 'Alert!'],
- ]);
- $expected = [
- 'div' => ['id' => 'notificationLayout'],
- '<h1', 'Alert!', '/h1',
- '<h3', 'Notice!', '/h3',
- '<p', 'This is a test of the emergency broadcasting system', '/p',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test using elements in plugins.
- *
- * @return void
- */
- public function testFlashWithPluginElement()
- {
- $this->loadPlugins(['TestPlugin']);
- $result = $this->Flash->render('flash', ['element' => 'TestPlugin.Flash/plugin_element']);
- $expected = 'this is the plugin element';
- $this->assertEquals($expected, $result);
- }
- /**
- * test that when View theme is set, flash element from that theme (plugin) is used.
- *
- * @return void
- */
- public function testFlashWithTheme()
- {
- $this->loadPlugins(['TestTheme']);
- $this->View->setTheme('TestTheme');
- $result = $this->Flash->render('flash');
- $expected = 'flash element from TestTheme';
- $this->assertContains($expected, $result);
- }
- /**
- * Test that when rendering a stack, messages are displayed in their
- * respective element, in the order they were added in the stack
- *
- * @return void
- */
- public function testFlashWithStack()
- {
- $result = $this->Flash->render('stack');
- $expected = [
- ['div' => ['class' => 'message']], 'This is a calling', '/div',
- ['div' => ['id' => 'notificationLayout']],
- '<h1', 'Alert!', '/h1',
- '<h3', 'Notice!', '/h3',
- '<p', 'This is a test of the emergency broadcasting system', '/p',
- '/div',
- ['div' => ['id' => 'classy-message']], 'Recorded', '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->assertNull($this->View->getRequest()->getSession()->read('Flash.stack'));
- }
- /**
- * test that when View prefix is set, flash element from that prefix
- * is used if available.
- *
- * @return void
- */
- public function testFlashWithPrefix()
- {
- $this->View->setRequest($this->View->getRequest()->withParam('prefix', 'Admin'));
- $result = $this->Flash->render('flash');
- $expected = 'flash element from Admin prefix folder';
- $this->assertContains($expected, $result);
- }
- }
|