* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\View\Helper;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Model\Model;
use Cake\Network\Request;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use Cake\Utility\ClassRegistry;
use Cake\Utility\File;
use Cake\Utility\Folder;
use Cake\View\Helper\FormHelper;
use Cake\View\Helper\HtmlHelper;
use Cake\View\View;
/**
* HtmlHelperTest class
*
*/
class HtmlHelperTest extends TestCase {
/**
* Regexp for CDATA start block
*
* @var string
*/
public $cDataStart = 'preg:/^\/\/[\s\r\n]*/';
/**
* html property
*
* @var object
*/
public $Html = null;
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$controller = $this->getMock('Cake\Controller\Controller');
$this->View = $this->getMock('Cake\View\View', array('append'));
$this->Html = new HtmlHelper($this->View);
$this->Html->request = new Request();
$this->Html->request->webroot = '';
Configure::write('Asset.timestamp', false);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
unset($this->Html, $this->View);
}
/**
* testDocType method
*
* @return void
*/
public function testDocType() {
$result = $this->Html->docType();
$expected = '';
$this->assertEquals($expected, $result);
$result = $this->Html->docType('html4-strict');
$expected = '';
$this->assertEquals($expected, $result);
$this->assertNull($this->Html->docType('non-existing-doctype'));
}
/**
* testLink method
*
* @return void
*/
public function testLink() {
Router::connect('/:controller/:action/*');
$this->Html->request->webroot = '';
$result = $this->Html->link('/home');
$expected = array('a' => array('href' => '/home'), 'preg:/\/home/', '/a');
$this->assertTags($result, $expected);
$result = $this->Html->link(array('action' => 'login', '<[You]>'));
$expected = array(
'a' => array('href' => '/login/%3C%5BYou%5D%3E'),
'preg:/\/login\/<\[You\]>/',
'/a'
);
$this->assertTags($result, $expected);
Router::reload();
Router::connect('/:controller', array('action' => 'index'));
Router::connect('/:controller/:action/*');
$result = $this->Html->link('Posts', array('controller' => 'posts', 'action' => 'index', '_full' => true));
$expected = array('a' => array('href' => Router::fullBaseUrl() . '/posts'), 'Posts', '/a');
$this->assertTags($result, $expected);
$result = $this->Html->link('Home', '/home', array('confirm' => 'Are you sure you want to do this?'));
$expected = array(
'a' => array('href' => '/home', 'onclick' => 'if (confirm("Are you sure you want to do this?")) { return true; } return false;'),
'Home',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Home', '/home', array('escape' => false, 'confirm' => 'Confirm\'s "nightmares"'));
$expected = array(
'a' => array('href' => '/home', 'onclick' => 'if (confirm("Confirm's \"nightmares\"")) { return true; } return false;'),
'Home',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Home', '/home', array('default' => false));
$expected = array(
'a' => array('href' => '/home', 'onclick' => 'event.returnValue = false; return false;'),
'Home',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Home', '/home', array('default' => false, 'onclick' => 'someFunction();'));
$expected = array(
'a' => array('href' => '/home', 'onclick' => 'someFunction(); event.returnValue = false; return false;'),
'Home',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Next >', '#');
$expected = array(
'a' => array('href' => '#'),
'Next >',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Next >', '#', array('escape' => true));
$expected = array(
'a' => array('href' => '#'),
'Next >',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Next >', '#', array('escape' => 'utf-8'));
$expected = array(
'a' => array('href' => '#'),
'Next >',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Next >', '#', array('escape' => false));
$expected = array(
'a' => array('href' => '#'),
'Next >',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Next >', '#', array(
'title' => 'to escape … or not escape?',
'escape' => false
));
$expected = array(
'a' => array('href' => '#', 'title' => 'to escape … or not escape?'),
'Next >',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Next >', '#', array(
'title' => 'to escape … or not escape?',
'escape' => true
));
$expected = array(
'a' => array('href' => '#', 'title' => 'to escape … or not escape?'),
'Next >',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Next >', '#', array(
'title' => 'Next >',
'escapeTitle' => false
));
$expected = array(
'a' => array('href' => '#', 'title' => 'Next >'),
'Next >',
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('Original size', array(
'controller' => 'images', 'action' => 'view', 3, '?' => array('height' => 100, 'width' => 200)
));
$expected = array(
'a' => array('href' => '/images/view/3?height=100&width=200'),
'Original size',
'/a'
);
$this->assertTags($result, $expected);
Configure::write('Asset.timestamp', false);
$result = $this->Html->link($this->Html->image('test.gif'), '#', array('escape' => false));
$expected = array(
'a' => array('href' => '#'),
'img' => array('src' => 'img/test.gif', 'alt' => ''),
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link($this->Html->image('test.gif'), '#', array(
'title' => 'hey "howdy"',
'escapeTitle' => false
));
$expected = array(
'a' => array('href' => '#', 'title' => 'hey "howdy"'),
'img' => array('src' => 'img/test.gif', 'alt' => ''),
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->image('test.gif', array('url' => '#'));
$expected = array(
'a' => array('href' => '#'),
'img' => array('src' => 'img/test.gif', 'alt' => ''),
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link($this->Html->image('../favicon.ico'), '#', array('escape' => false));
$expected = array(
'a' => array('href' => '#'),
'img' => array('src' => 'img/../favicon.ico', 'alt' => ''),
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->image('../favicon.ico', array('url' => '#'));
$expected = array(
'a' => array('href' => '#'),
'img' => array('src' => 'img/../favicon.ico', 'alt' => ''),
'/a'
);
$this->assertTags($result, $expected);
$result = $this->Html->link('http://www.example.org?param1=value1¶m2=value2');
$expected = array('a' => array('href' => 'http://www.example.org?param1=value1¶m2=value2'), 'http://www.example.org?param1=value1¶m2=value2', '/a');
$this->assertTags($result, $expected);
$result = $this->Html->link('alert', 'javascript:alert(\'cakephp\');');
$expected = array('a' => array('href' => 'javascript:alert('cakephp');'), 'alert', '/a');
$this->assertTags($result, $expected);
$result = $this->Html->link('write me', 'mailto:example@cakephp.org');
$expected = array('a' => array('href' => 'mailto:example@cakephp.org'), 'write me', '/a');
$this->assertTags($result, $expected);
$result = $this->Html->link('call me on 0123465-798', 'tel:0123465-798');
$expected = array('a' => array('href' => 'tel:0123465-798'), 'call me on 0123465-798', '/a');
$this->assertTags($result, $expected);
$result = $this->Html->link('text me on 0123465-798', 'sms:0123465-798');
$expected = array('a' => array('href' => 'sms:0123465-798'), 'text me on 0123465-798', '/a');
$this->assertTags($result, $expected);
$result = $this->Html->link('say hello to 0123465-798', 'sms:0123465-798?body=hello there');
$expected = array('a' => array('href' => 'sms:0123465-798?body=hello there'), 'say hello to 0123465-798', '/a');
$this->assertTags($result, $expected);
$result = $this->Html->link('say hello to 0123465-798', 'sms:0123465-798?body=hello "cakephp"');
$expected = array('a' => array('href' => 'sms:0123465-798?body=hello "cakephp"'), 'say hello to 0123465-798', '/a');
$this->assertTags($result, $expected);
}
/**
* testImageTag method
*
* @return void
*/
public function testImageTag() {
Router::connect('/:controller', array('action' => 'index'));
Router::connect('/:controller/:action/*');
$this->Html->request->webroot = '';
$result = $this->Html->image('test.gif');
$this->assertTags($result, array('img' => array('src' => 'img/test.gif', 'alt' => '')));
$result = $this->Html->image('http://google.com/logo.gif');
$this->assertTags($result, array('img' => array('src' => 'http://google.com/logo.gif', 'alt' => '')));
$result = $this->Html->image('//google.com/logo.gif');
$this->assertTags($result, array('img' => array('src' => '//google.com/logo.gif', 'alt' => '')));
$result = $this->Html->image(array('controller' => 'test', 'action' => 'view', 1, 'ext' => 'gif'));
$this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
$result = $this->Html->image('/test/view/1.gif');
$this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
}
/**
* Test image() with query strings.
*
* @return void
*/
public function testImageQueryString() {
$result = $this->Html->image('test.gif?one=two&three=four');
$this->assertTags($result, array('img' => array('src' => 'img/test.gif?one=two&three=four', 'alt' => '')));
$result = $this->Html->image(array(
'controller' => 'images',
'action' => 'display',
'test',
'?' => array('one' => 'two', 'three' => 'four')
));
$this->assertTags($result, array('img' => array('src' => '/images/display/test?one=two&three=four', 'alt' => '')));
}
/**
* Test that image works with pathPrefix.
*
* @return void
*/
public function testImagePathPrefix() {
$result = $this->Html->image('test.gif', array('pathPrefix' => '/my/custom/path/'));
$this->assertTags($result, array('img' => array('src' => '/my/custom/path/test.gif', 'alt' => '')));
$result = $this->Html->image('test.gif', array('pathPrefix' => 'http://cakephp.org/assets/img/'));
$this->assertTags($result, array('img' => array('src' => 'http://cakephp.org/assets/img/test.gif', 'alt' => '')));
$result = $this->Html->image('test.gif', array('pathPrefix' => '//cakephp.org/assets/img/'));
$this->assertTags($result, array('img' => array('src' => '//cakephp.org/assets/img/test.gif', 'alt' => '')));
$previousConfig = Configure::read('App.imageBaseUrl');
Configure::write('App.imageBaseUrl', '//cdn.cakephp.org/img/');
$result = $this->Html->image('test.gif');
$this->assertTags($result, array('img' => array('src' => '//cdn.cakephp.org/img/test.gif', 'alt' => '')));
Configure::write('App.imageBaseUrl', $previousConfig);
}
/**
* Test that image() works with fullBase and a webroot not equal to /
*
* @return void
*/
public function testImageWithFullBase() {
$result = $this->Html->image('test.gif', array('fullBase' => true));
$here = $this->Html->url('/', true);
$this->assertTags($result, array('img' => array('src' => $here . 'img/test.gif', 'alt' => '')));
$result = $this->Html->image('sub/test.gif', array('fullBase' => true));
$here = $this->Html->url('/', true);
$this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
$request = $this->Html->request;
$request->webroot = '/myproject/';
$request->base = '/myproject';
Router::pushRequest($request);
$result = $this->Html->image('sub/test.gif', array('fullBase' => true));
$here = $this->Html->url('/', true);
$this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
}
/**
* test image() with Asset.timestamp
*
* @return void
*/
public function testImageWithTimestampping() {
Configure::write('Asset.timestamp', 'force');
$this->Html->request->webroot = '/';
$result = $this->Html->image('cake.icon.png');
$this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
Configure::write('debug', false);
Configure::write('Asset.timestamp', 'force');
$result = $this->Html->image('cake.icon.png');
$this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
$this->Html->request->webroot = '/testing/longer/';
$result = $this->Html->image('cake.icon.png');
$expected = array(
'img' => array('src' => 'preg:/\/testing\/longer\/img\/cake\.icon\.png\?[0-9]+/', 'alt' => '')
);
$this->assertTags($result, $expected);
}
/**
* Tests creation of an image tag using a theme and asset timestamping
*
* @return void
*/
public function testImageTagWithTheme() {
$this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
$testfile = WWW_ROOT . 'theme/test_theme/img/__cake_test_image.gif';
new File($testfile, true);
Configure::write('Asset.timestamp', true);
Configure::write('debug', true);
$this->Html->request->webroot = '/';
$this->Html->theme = 'test_theme';
$result = $this->Html->image('__cake_test_image.gif');
$this->assertTags($result, array(
'img' => array(
'src' => 'preg:/\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
'alt' => ''
)));
$this->Html->request->webroot = '/testing/';
$result = $this->Html->image('__cake_test_image.gif');
$this->assertTags($result, array(
'img' => array(
'src' => 'preg:/\/testing\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
'alt' => ''
)));
}
/**
* test theme assets in main webroot path
*
* @return void
*/
public function testThemeAssetsInMainWebrootPath() {
$webRoot = Configure::read('App.www_root');
Configure::write('App.www_root', TEST_APP . 'webroot/');
$this->Html->theme = 'test_theme';
$result = $this->Html->css('webroot_test');
$expected = array(
'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*theme\/test_theme\/css\/webroot_test\.css/')
);
$this->assertTags($result, $expected);
$this->Html->theme = 'test_theme';
$result = $this->Html->css('theme_webroot');
$expected = array(
'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*theme\/test_theme\/css\/theme_webroot\.css/')
);
$this->assertTags($result, $expected);
}
/**
* testStyle method
*
* @return void
*/
public function testStyle() {
$result = $this->Html->style(array('display' => 'none', 'margin' => '10px'));
$expected = 'display:none; margin:10px;';
$this->assertRegExp('/^display\s*:\s*none\s*;\s*margin\s*:\s*10px\s*;?$/', $expected);
$result = $this->Html->style(array('display' => 'none', 'margin' => '10px'), false);
$lines = explode("\n", $result);
$this->assertRegExp('/^\s*display\s*:\s*none\s*;\s*$/', $lines[0]);
$this->assertRegExp('/^\s*margin\s*:\s*10px\s*;?$/', $lines[1]);
}
/**
* testCssLink method
*
* @return void
*/
public function testCssLink() {
$result = $this->Html->css('screen');
$expected = array(
'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/')
);
$this->assertTags($result, $expected);
$result = $this->Html->css('screen.css');
$this->assertTags($result, $expected);
Plugin::load('TestPlugin');
$result = $this->Html->css('TestPlugin.style', array('plugin' => false));
$expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
$this->assertTags($result, $expected);
Plugin::unload('TestPlugin');
$result = $this->Html->css('my.css.library');
$expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
$this->assertTags($result, $expected);
$result = $this->Html->css('screen.css?1234');
$expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
$this->assertTags($result, $expected);
$result = $this->Html->css('screen.css?with=param&other=param');
$expected['link']['href'] = 'css/screen.css?with=param&other=param';
$this->assertTags($result, $expected);
$result = $this->Html->css('http://whatever.com/screen.css?1234');
$expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
$this->assertTags($result, $expected);
Configure::write('App.cssBaseUrl', '//cdn.cakephp.org/css/');
$result = $this->Html->css('cake.generic');
$expected['link']['href'] = '//cdn.cakephp.org/css/cake.generic.css';
$this->assertTags($result, $expected);
$result = $this->Html->css('//example.com/css/cake.generic.css');
$expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
$this->assertTags($result, $expected);
$result = explode("\n", trim($this->Html->css(array('cake.generic', 'vendor.generic'))));
$expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
$this->assertTags($result[0], $expected);
$expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
$this->assertTags($result[1], $expected);
$this->assertEquals(2, count($result));
$this->View->expects($this->at(0))
->method('append')
->with('css', $this->matchesRegularExpression('/css_in_head.css/'));
$this->View->expects($this->at(1))
->method('append')
->with('css', $this->matchesRegularExpression('/more_css_in_head.css/'));
$result = $this->Html->css('css_in_head', array('block' => true));
$this->assertNull($result);
$result = $this->Html->css('more_css_in_head', array('block' => true));
$this->assertNull($result);
$result = $this->Html->css('screen', array('rel' => 'import'));
$expected = array(
'