cakephp2-php8/lib/Cake/Test/Case/View/Helper/FlashHelperTest.php

187 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* FlashHelperTest file
*
* Series of tests for flash helper.
*
2017-06-10 22:15:34 +00:00
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
* 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)
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.View.Helper
* @since CakePHP(tm) v 2.7.0-dev
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
App::uses('FlashHelper', 'View/Helper');
App::uses('View', 'View');
App::uses('CakePlugin', 'Core');
/**
2015-02-04 15:31:50 +00:00
* FlashHelperTest class
*
* @package Cake.Test.Case.View.Helper
*/
class FlashHelperTest extends CakeTestCase {
2015-02-04 15:31:50 +00:00
/**
* setupBeforeClass method
*
* @return void
*/
public static function setupBeforeClass() : void {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
));
}
2015-02-04 15:31:50 +00:00
/**
* setUp method
*
* @return void
*/
public function setUp() : void {
parent::setUp();
$controller = null;
$this->View = new View($controller);
$this->Flash = new FlashHelper($this->View);
if (!CakeSession::started()) {
CakeSession::start();
}
CakeSession::write(array(
'Message' => array(
'flash' => array(
2016-10-27 23:10:52 +00:00
array(
'key' => 'flash',
'message' => 'This is the first Message',
'element' => 'Flash/default',
'params' => array()
),
array(
'key' => 'flash',
'message' => 'This is the second Message',
'element' => 'Flash/default',
'params' => array()
)
),
'notification' => array(
2016-10-27 23:10:52 +00:00
array(
'key' => 'notification',
'message' => 'Broadcast message testing',
'element' => 'flash_helper',
'params' => array(
'title' => 'Notice!',
'name' => 'Alert!'
)
)
),
'classy' => array(
2016-10-27 23:10:52 +00:00
array(
'key' => 'classy',
'message' => 'Recorded',
'element' => 'flash_classy',
'params' => array()
)
),
'default' => array(
array(
'key' => 'default',
'message' => 'Default',
'element' => 'default',
'params' => array()
)
)
)
));
}
2015-02-04 15:31:50 +00:00
/**
* tearDown method
*
* @return void
*/
public function tearDown() : void {
parent::tearDown();
unset($this->View, $this->Flash);
CakeSession::destroy();
}
2015-02-04 15:31:50 +00:00
/**
* testFlash method
*
* @return void
*/
public function testFlash() {
$result = $this->Flash->render();
2016-10-27 23:10:52 +00:00
$expected = '<div class="message">This is the first Message</div><div class="message">This is the second Message</div>';
$this->assertStringContainsString($expected, $result);
$expected = '<div id="classy-message">Recorded</div>';
$result = $this->Flash->render('classy');
$this->assertStringContainsString($expected, $result);
$result = $this->Flash->render('notification');
$expected = "<div id=\"notificationLayout\">\n\t<h1>Alert!</h1>\n\t<h3>Notice!</h3>\n\t<p>Broadcast message testing</p>\n</div>";
$this->assertStringContainsString($expected, $result);
$this->assertNull($this->Flash->render('non-existent'));
}
2015-02-04 15:31:50 +00:00
/**
* testFlashThrowsException
*
*/
public function testFlashThrowsException() {
$this->expectException(UnexpectedValueException::class);
CakeSession::write('Message.foo', 'bar');
$this->Flash->render('foo');
}
2015-02-04 15:31:50 +00:00
/**
* test setting the element from the attrs.
*
* @return void
*/
public function testFlashElementInAttrs() {
$result = $this->Flash->render('notification', array(
'element' => 'flash_helper',
'params' => array('title' => 'Alert!', 'name' => 'Notice!')
));
$expected = "<div id=\"notificationLayout\">\n\t<h1>Notice!</h1>\n\t<h3>Alert!</h3>\n\t<p>Broadcast message testing</p>\n</div>";
$this->assertStringContainsString($expected, $result);
}
2015-02-04 15:31:50 +00:00
/**
* test using elements in plugins.
*
* @return void
*/
public function testFlashWithPluginElement() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));
CakePlugin::load('TestPlugin');
$result = $this->Flash->render('flash', array('element' => 'TestPlugin.plugin_element'));
$expected = 'this is the plugin element';
$this->assertStringContainsString($expected, $result);
}
/**
* Test that the default element fallbacks to the Flash/default element.
*/
public function testFlashFallback() {
$result = $this->Flash->render('default');
$expected = '<div class="message">Default</div>';
$this->assertStringContainsString($expected, $result);
}
}