2015-02-04 15:05:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FlashComponentTest file
|
|
|
|
*
|
|
|
|
* Series of tests for flash component.
|
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2015-02-04 15:05:40 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @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
|
2015-02-04 15:05:40 +00:00
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
* @since CakePHP(tm) v 2.7.0-dev
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2015-02-04 15:05:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('FlashComponent', 'Controller/Component');
|
|
|
|
App::uses('ComponentCollection', 'Controller');
|
|
|
|
|
|
|
|
/**
|
2015-02-04 15:31:50 +00:00
|
|
|
* FlashComponentTest class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Controller.Component
|
|
|
|
*/
|
2015-02-04 15:05:40 +00:00
|
|
|
class FlashComponentTest extends CakeTestCase {
|
|
|
|
|
|
|
|
/**
|
2015-02-04 15:31:50 +00:00
|
|
|
* setUp method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function setUp() : void {
|
2015-02-04 15:05:40 +00:00
|
|
|
parent::setUp();
|
|
|
|
$this->Components = new ComponentCollection();
|
|
|
|
$this->Flash = new FlashComponent($this->Components);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-04 15:31:50 +00:00
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function tearDown() : void {
|
2015-02-04 15:05:40 +00:00
|
|
|
parent::tearDown();
|
|
|
|
CakeSession::destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-04 15:31:50 +00:00
|
|
|
* testSet method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-02-04 15:05:40 +00:00
|
|
|
public function testSet() {
|
2015-06-24 11:06:35 +00:00
|
|
|
$this->assertNull(CakeSession::read('Message.flash'));
|
2015-02-04 15:05:40 +00:00
|
|
|
|
|
|
|
$this->Flash->set('This is a test message');
|
|
|
|
$expected = array(
|
2016-10-27 21:39:21 +00:00
|
|
|
array(
|
|
|
|
'message' => 'This is a test message',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/default',
|
|
|
|
'params' => array()
|
|
|
|
)
|
2015-02-04 15:05:40 +00:00
|
|
|
);
|
2015-06-24 11:06:35 +00:00
|
|
|
$result = CakeSession::read('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.flash');
|
|
|
|
|
|
|
|
$this->Flash->set('This is the first message');
|
|
|
|
$this->Flash->set('This is the second message');
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'message' => 'This is the first message',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/default',
|
|
|
|
'params' => array()
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'message' => 'This is the second message',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/default',
|
|
|
|
'params' => array()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = CakeSession::read('Message.flash');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
CakeSession::delete('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
|
|
|
|
$this->Flash->set('This is a test message', array(
|
|
|
|
'element' => 'test',
|
|
|
|
'params' => array('foo' => 'bar')
|
|
|
|
));
|
|
|
|
$expected = array(
|
2016-10-27 21:39:21 +00:00
|
|
|
array(
|
|
|
|
'message' => 'This is a test message',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/test',
|
|
|
|
'params' => array('foo' => 'bar')
|
|
|
|
)
|
2015-02-04 15:05:40 +00:00
|
|
|
);
|
2015-06-24 11:06:35 +00:00
|
|
|
$result = CakeSession::read('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
|
|
|
|
$this->Flash->set('This is a test message', array('element' => 'MyPlugin.alert'));
|
|
|
|
$expected = array(
|
2016-10-27 21:39:21 +00:00
|
|
|
array(
|
|
|
|
'message' => 'This is a test message',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'MyPlugin.Flash/alert',
|
|
|
|
'params' => array()
|
|
|
|
)
|
2015-02-04 15:05:40 +00:00
|
|
|
);
|
2015-06-24 11:06:35 +00:00
|
|
|
$result = CakeSession::read('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
|
|
|
|
$this->Flash->set('This is a test message', array('key' => 'foobar'));
|
|
|
|
$expected = array(
|
2016-10-27 21:39:21 +00:00
|
|
|
array(
|
|
|
|
'message' => 'This is a test message',
|
|
|
|
'key' => 'foobar',
|
|
|
|
'element' => 'Flash/default',
|
|
|
|
'params' => array()
|
|
|
|
)
|
2015-02-04 15:05:40 +00:00
|
|
|
);
|
2015-06-24 11:06:35 +00:00
|
|
|
$result = CakeSession::read('Message.foobar');
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.foobar');
|
2016-10-31 03:39:00 +00:00
|
|
|
|
|
|
|
$this->Flash->set('This is the first message');
|
|
|
|
$this->Flash->set('This is the second message', array('clear' => true));
|
|
|
|
$expected = array(
|
|
|
|
array(
|
|
|
|
'message' => 'This is the second message',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/default',
|
|
|
|
'params' => array()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = CakeSession::read('Message.flash');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
CakeSession::delete('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-04 15:31:50 +00:00
|
|
|
* testSetWithException method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-02-04 15:05:40 +00:00
|
|
|
public function testSetWithException() {
|
2015-06-24 11:06:35 +00:00
|
|
|
$this->assertNull(CakeSession::read('Message.flash'));
|
2015-02-04 15:05:40 +00:00
|
|
|
|
|
|
|
$this->Flash->set(new Exception('This is a test message', 404));
|
|
|
|
$expected = array(
|
2016-10-27 21:39:21 +00:00
|
|
|
array(
|
|
|
|
'message' => 'This is a test message',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/default',
|
|
|
|
'params' => array('code' => 404)
|
|
|
|
)
|
2015-02-04 15:05:40 +00:00
|
|
|
);
|
2015-06-24 11:06:35 +00:00
|
|
|
$result = CakeSession::read('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-04 15:31:50 +00:00
|
|
|
* testSetWithComponentConfiguration method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-02-04 15:05:40 +00:00
|
|
|
public function testSetWithComponentConfiguration() {
|
2015-06-24 11:06:35 +00:00
|
|
|
$this->assertNull(CakeSession::read('Message.flash'));
|
2015-02-04 15:05:40 +00:00
|
|
|
|
|
|
|
$FlashWithSettings = $this->Components->load('Flash', array('element' => 'test'));
|
|
|
|
$FlashWithSettings->set('This is a test message');
|
|
|
|
$expected = array(
|
2016-10-27 21:39:21 +00:00
|
|
|
array(
|
|
|
|
'message' => 'This is a test message',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/test',
|
|
|
|
'params' => array()
|
|
|
|
)
|
2015-02-04 15:05:40 +00:00
|
|
|
);
|
2015-06-24 11:06:35 +00:00
|
|
|
$result = CakeSession::read('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-04 15:31:50 +00:00
|
|
|
* Test magic call method.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-02-04 15:05:40 +00:00
|
|
|
public function testCall() {
|
2015-06-24 11:06:35 +00:00
|
|
|
$this->assertNull(CakeSession::read('Message.flash'));
|
2015-02-04 15:05:40 +00:00
|
|
|
|
|
|
|
$this->Flash->success('It worked');
|
|
|
|
$expected = array(
|
2016-10-27 23:10:52 +00:00
|
|
|
array(
|
|
|
|
'message' => 'It worked',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/success',
|
|
|
|
'params' => array()
|
|
|
|
)
|
2015-02-04 15:05:40 +00:00
|
|
|
);
|
2015-06-24 11:06:35 +00:00
|
|
|
$result = CakeSession::read('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
|
2015-10-18 00:54:40 +00:00
|
|
|
$this->Flash->alert('It worked', array('plugin' => 'MyPlugin'));
|
|
|
|
$expected = array(
|
2016-10-27 23:10:52 +00:00
|
|
|
array(
|
|
|
|
'message' => 'It worked',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'MyPlugin.Flash/alert',
|
|
|
|
'params' => array()
|
|
|
|
)
|
2015-10-18 00:54:40 +00:00
|
|
|
);
|
|
|
|
$result = CakeSession::read('Message.flash');
|
|
|
|
$this->assertEquals($expected, $result);
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.flash');
|
2015-10-18 00:54:40 +00:00
|
|
|
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->Flash->error('It did not work', array('element' => 'error_thing'));
|
|
|
|
$expected = array(
|
2016-10-27 23:10:52 +00:00
|
|
|
array(
|
|
|
|
'message' => 'It did not work',
|
|
|
|
'key' => 'flash',
|
|
|
|
'element' => 'Flash/error',
|
|
|
|
'params' => array()
|
|
|
|
)
|
2015-02-04 15:05:40 +00:00
|
|
|
);
|
2015-06-24 11:06:35 +00:00
|
|
|
$result = CakeSession::read('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
$this->assertEquals($expected, $result, 'Element is ignored in magic call.');
|
2016-10-27 23:10:52 +00:00
|
|
|
CakeSession::delete('Message.flash');
|
2015-02-04 15:05:40 +00:00
|
|
|
}
|
|
|
|
}
|