| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315 |
- <?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 1.2.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\View\Helper;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\Filesystem\File;
- use Cake\Http\ServerRequest;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- use Cake\View\Helper\HtmlHelper;
- /**
- * HtmlHelperTest class
- */
- class HtmlHelperTest extends TestCase
- {
- /**
- * Regexp for CDATA start block
- *
- * @var string
- */
- public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
- /**
- * Regexp for CDATA end block
- *
- * @var string
- */
- public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
- /**
- * Helper to be tested
- *
- * @var \Cake\View\Helper\HtmlHelper
- */
- public $Html;
- /**
- * Mocked view
- *
- * @var \Cake\View\View|\PHPUnit_Framework_MockObject_MockObject
- */
- public $View;
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- $request = new ServerRequest([
- 'webroot' => '',
- ]);
- $this->View = $this->getMockBuilder('Cake\View\View')
- ->setMethods(['append'])
- ->setConstructorArgs([$request])
- ->getMock();
- $this->Html = new HtmlHelper($this->View);
- $this->loadPlugins(['TestTheme']);
- static::setAppNamespace();
- Configure::write('Asset.timestamp', false);
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- $this->clearPlugins();
- unset($this->Html, $this->View);
- }
- /**
- * testDocType method
- *
- * @return void
- */
- public function testDocType()
- {
- $result = $this->Html->docType();
- $expected = '<!DOCTYPE html>';
- $this->assertEquals($expected, $result);
- $result = $this->Html->docType('html4-strict');
- $expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
- $this->assertEquals($expected, $result);
- $this->assertNull($this->Html->docType('non-existing-doctype'));
- }
- /**
- * testLink method
- *
- * @return void
- */
- public function testLink()
- {
- Router::reload();
- Router::connect('/:controller/:action/*');
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', ''));
- $result = $this->Html->link('/home');
- $expected = ['a' => ['href' => '/home'], 'preg:/\/home/', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link(['action' => 'login', '<[You]>']);
- $expected = [
- 'a' => ['href' => '/login/%3C%5BYou%5D%3E'],
- 'preg:/\/login\/<\[You\]>/',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- Router::reload();
- Router::connect('/:controller', ['action' => 'index']);
- Router::connect('/:controller/:action/*');
- $result = $this->Html->link('Posts', ['controller' => 'posts', 'action' => 'index', '_full' => true]);
- $expected = ['a' => ['href' => Router::fullBaseUrl() . '/posts'], 'Posts', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Home', '/home', ['confirm' => 'Are you sure you want to do this?']);
- $expected = [
- 'a' => ['href' => '/home', 'onclick' => 'if (confirm("Are you sure you want to do this?")) { return true; } return false;'],
- 'Home',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $this->Html->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { window.location="/";};']);
- $result = $this->Html->link('Home', '/home', ['confirm' => 'Are you sure you want to do this?']);
- $expected = [
- 'a' => ['href' => '/home', 'onclick' => 'preg:/if \(confirm\("Are you sure you want to do this\?"\)\) \{ window\.location="\/";\};/'],
- 'Home',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $this->Html->setTemplates(['confirmJs' => '{{confirm}}']);
- $result = $this->Html->link('Home', '/home', ['escape' => false, 'confirm' => 'Confirm\'s "nightmares"']);
- $expected = [
- 'a' => ['href' => '/home', 'onclick' => 'if (confirm("Confirm's \"nightmares\"")) { return true; } return false;'],
- 'Home',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Home', '/home', ['onclick' => 'someFunction();']);
- $expected = [
- 'a' => ['href' => '/home', 'onclick' => 'someFunction();'],
- 'Home',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Next >', '#');
- $expected = [
- 'a' => ['href' => '#'],
- 'Next >',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Next >', '#', ['escape' => true]);
- $expected = [
- 'a' => ['href' => '#'],
- 'Next >',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Next >', '#', ['escape' => 'utf-8']);
- $expected = [
- 'a' => ['href' => '#'],
- 'Next >',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Next >', '#', ['escape' => false]);
- $expected = [
- 'a' => ['href' => '#'],
- 'Next >',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Next >', '#', [
- 'title' => 'to escape … or not escape?',
- 'escape' => false,
- ]);
- $expected = [
- 'a' => ['href' => '#', 'title' => 'to escape … or not escape?'],
- 'Next >',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Next >', '#', [
- 'title' => 'to escape … or not escape?',
- 'escape' => true,
- ]);
- $expected = [
- 'a' => ['href' => '#', 'title' => 'to escape &#8230; or not escape?'],
- 'Next >',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Next >', '#', [
- 'title' => 'Next >',
- 'escapeTitle' => false,
- ]);
- $expected = [
- 'a' => ['href' => '#', 'title' => 'Next >'],
- 'Next >',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Original size', [
- 'controller' => 'images', 'action' => 'view', 3, '?' => ['height' => 100, 'width' => 200],
- ]);
- $expected = [
- 'a' => ['href' => '/images/view/3?height=100&width=200'],
- 'Original size',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- Configure::write('Asset.timestamp', false);
- $result = $this->Html->link($this->Html->image('test.gif'), '#', ['escape' => false]);
- $expected = [
- 'a' => ['href' => '#'],
- 'img' => ['src' => 'img/test.gif', 'alt' => ''],
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link($this->Html->image('test.gif'), '#', [
- 'title' => 'hey "howdy"',
- 'escapeTitle' => false,
- ]);
- $expected = [
- 'a' => ['href' => '#', 'title' => 'hey "howdy"'],
- 'img' => ['src' => 'img/test.gif', 'alt' => ''],
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('test.gif', ['url' => '#']);
- $expected = [
- 'a' => ['href' => '#'],
- 'img' => ['src' => 'img/test.gif', 'alt' => ''],
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link($this->Html->image('../favicon.ico'), '#', ['escape' => false]);
- $expected = [
- 'a' => ['href' => '#'],
- 'img' => ['src' => 'img/../favicon.ico', 'alt' => ''],
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('../favicon.ico', ['url' => '#']);
- $expected = [
- 'a' => ['href' => '#'],
- 'img' => ['src' => 'img/../favicon.ico', 'alt' => ''],
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('http://www.example.org?param1=value1¶m2=value2');
- $expected = ['a' => ['href' => 'http://www.example.org?param1=value1&param2=value2'], 'http://www.example.org?param1=value1&param2=value2', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('alert', 'javascript:alert(\'cakephp\');');
- $expected = ['a' => ['href' => 'javascript:alert('cakephp');'], 'alert', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('write me', 'mailto:example@cakephp.org');
- $expected = ['a' => ['href' => 'mailto:example@cakephp.org'], 'write me', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('call me on 0123465-798', 'tel:0123465-798');
- $expected = ['a' => ['href' => 'tel:0123465-798'], 'call me on 0123465-798', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('text me on 0123465-798', 'sms:0123465-798');
- $expected = ['a' => ['href' => 'sms:0123465-798'], 'text me on 0123465-798', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('say hello to 0123465-798', 'sms:0123465-798?body=hello there');
- $expected = ['a' => ['href' => 'sms:0123465-798?body=hello there'], 'say hello to 0123465-798', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('say hello to 0123465-798', 'sms:0123465-798?body=hello "cakephp"');
- $expected = ['a' => ['href' => 'sms:0123465-798?body=hello "cakephp"'], 'say hello to 0123465-798', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Home', '/', ['fullBase' => true]);
- $expected = ['a' => ['href' => 'http://localhost/'], 'Home', '/a'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->link('Home', '/', ['fullBase' => false]);
- $expected = ['a' => ['href' => '/'], 'Home', '/a'];
- $this->assertHtml($expected, $result);
- }
- /**
- * testImageTag method
- *
- * @return void
- */
- public function testImageTag()
- {
- Router::connect('/:controller', ['action' => 'index']);
- Router::connect('/:controller/:action/*');
- $result = $this->Html->image('test.gif');
- $expected = ['img' => ['src' => 'img/test.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('http://google.com/logo.gif');
- $expected = ['img' => ['src' => 'http://google.com/logo.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('//google.com/logo.gif');
- $expected = ['img' => ['src' => '//google.com/logo.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image(['controller' => 'test', 'action' => 'view', 1, '_ext' => 'gif']);
- $expected = ['img' => ['src' => '/test/view/1.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('/test/view/1.gif');
- $expected = ['img' => ['src' => '/test/view/1.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('cid:cakephp_logo');
- $expected = ['img' => ['src' => 'cid:cakephp_logo', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('x:"><script>alert(1)</script>');
- $expected = ['img' => ['src' => 'x:"><script>alert(1)</script>', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('//google.com/"><script>alert(1)</script>');
- $expected = ['img' => ['src' => '//google.com/"><script>alert(1)</script>', 'alt' => '']];
- $this->assertHtml($expected, $result);
- }
- /**
- * Ensure that data URIs don't get base paths set.
- *
- * @return void
- */
- public function testImageDataUriBaseDir()
- {
- $request = $this->View->getRequest()
- ->withAttribute('base', 'subdir')
- ->withAttribute('webroot', 'subdir/');
- $this->View->setRequest($request);
- Router::pushRequest($request);
- $data = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4' .
- '/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
- $result = $this->Html->image($data);
- $expected = ['img' => ['src' => $data, 'alt' => '']];
- $this->assertHtml($expected, $result);
- $data = 'data:image/png;base64,<evil>';
- $result = $this->Html->image($data);
- $expected = ['img' => ['src' => h($data), 'alt' => '']];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test image() with query strings.
- *
- * @return void
- */
- public function testImageQueryString()
- {
- $result = $this->Html->image('test.gif?one=two&three=four');
- $expected = ['img' => ['src' => 'img/test.gif?one=two&three=four', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image([
- 'controller' => 'images',
- 'action' => 'display',
- 'test',
- '?' => ['one' => 'two', 'three' => 'four'],
- ]);
- $expected = ['img' => ['src' => '/images/display/test?one=two&three=four', 'alt' => '']];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test that image works with pathPrefix.
- *
- * @return void
- */
- public function testImagePathPrefix()
- {
- $result = $this->Html->image('test.gif', ['pathPrefix' => '/my/custom/path/']);
- $expected = ['img' => ['src' => '/my/custom/path/test.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('test.gif', ['pathPrefix' => 'http://cakephp.org/assets/img/']);
- $expected = ['img' => ['src' => 'http://cakephp.org/assets/img/test.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('test.gif', ['pathPrefix' => '//cakephp.org/assets/img/']);
- $expected = ['img' => ['src' => '//cakephp.org/assets/img/test.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $previousConfig = Configure::read('App.imageBaseUrl');
- Configure::write('App.imageBaseUrl', '//cdn.cakephp.org/img/');
- $result = $this->Html->image('test.gif');
- $expected = ['img' => ['src' => '//cdn.cakephp.org/img/test.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- 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', ['fullBase' => true]);
- $here = $this->Html->Url->build('/', true);
- $expected = ['img' => ['src' => $here . 'img/test.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->image('sub/test.gif', ['fullBase' => true]);
- $here = $this->Html->Url->build('/', true);
- $expected = ['img' => ['src' => $here . 'img/sub/test.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()
- ->withAttribute('webroot', '/myproject/')
- ->withAttribute('base', '/myproject'));
- $result = $this->Html->image('sub/test.gif', ['fullBase' => true]);
- $here = $this->Html->Url->build('/', true);
- $expected = ['img' => ['src' => $here . 'myproject/img/sub/test.gif', 'alt' => '']];
- $this->assertHtml($expected, $result);
- }
- /**
- * test image() with Asset.timestamp
- *
- * @return void
- */
- public function testImageWithTimestampping()
- {
- Configure::write('Asset.timestamp', 'force');
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
- $result = $this->Html->image('cake.icon.png');
- $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
- $this->assertHtml($expected, $result);
- Configure::write('debug', false);
- Configure::write('Asset.timestamp', 'force');
- $result = $this->Html->image('cake.icon.png');
- $expected = ['img' => ['src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '']];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
- $result = $this->Html->image('cake.icon.png');
- $expected = [
- 'img' => ['src' => 'preg:/\/testing\/longer\/img\/cake\.icon\.png\?[0-9]+/', 'alt' => ''],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * 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 . 'test_theme/img/__cake_test_image.gif';
- $File = new File($testfile, true);
- Configure::write('Asset.timestamp', true);
- Configure::write('debug', true);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
- $this->Html->Url->getView()->setTheme('TestTheme');
- $result = $this->Html->image('__cake_test_image.gif');
- $expected = [
- 'img' => [
- 'src' => 'preg:/\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
- 'alt' => '',
- ],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
- $result = $this->Html->image('__cake_test_image.gif');
- $expected = [
- 'img' => [
- 'src' => 'preg:/\/testing\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
- 'alt' => '',
- ]];
- $this->assertHtml($expected, $result);
- $File->delete();
- }
- /**
- * test theme assets in main webroot path
- *
- * @return void
- */
- public function testThemeAssetsInMainWebrootPath()
- {
- Configure::write('App.wwwRoot', TEST_APP . 'webroot/');
- $this->Html->Url->getView()->setTheme('TestTheme');
- $result = $this->Html->css('webroot_test');
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_theme\/css\/webroot_test\.css/'],
- ];
- $this->assertHtml($expected, $result);
- $this->Html->getView()->setTheme('TestTheme');
- $result = $this->Html->css('theme_webroot');
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_theme\/css\/theme_webroot\.css/'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testStyle method
- *
- * @return void
- */
- public function testStyle()
- {
- $result = $this->Html->style(['display' => 'none', 'margin' => '10px']);
- $this->assertEquals('display:none; margin:10px;', $result);
- $result = $this->Html->style(['display' => 'none', 'margin' => '10px'], false);
- $this->assertEquals("display:none;\nmargin:10px;", $result);
- }
- /**
- * testCssLink method
- *
- * @return void
- */
- public function testCssLink()
- {
- $result = $this->Html->css('screen');
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->css('screen.css', ['once' => false]);
- $this->assertHtml($expected, $result);
- $this->loadPlugins(['TestPlugin']);
- $result = $this->Html->css('TestPlugin.style', ['plugin' => false]);
- $expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
- $this->assertHtml($expected, $result);
- $this->removePlugins(['TestPlugin']);
- $result = $this->Html->css('my.css.library');
- $expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
- $this->assertHtml($expected, $result);
- $result = $this->Html->css('screen.css?1234');
- $expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
- $this->assertHtml($expected, $result);
- $result = $this->Html->css('screen.css?with=param&other=param');
- $expected['link']['href'] = 'css/screen.css?with=param&other=param';
- $this->assertHtml($expected, $result);
- $result = $this->Html->css('x:"><script>alert(1)</script>');
- $expected['link']['href'] = 'x:"><script>alert(1)</script>';
- $this->assertHtml($expected, $result);
- $result = $this->Html->css('http://whatever.com/screen.css?1234');
- $expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
- $this->assertHtml($expected, $result);
- 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->assertHtml($expected, $result);
- $result = $this->Html->css('//example.com/css/cake.generic.css');
- $expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
- $this->assertHtml($expected, $result);
- $result = explode("\n", trim($this->Html->css(['cake', 'vendor.generic'])));
- $expected['link']['href'] = 'preg:/.*css\/cake\.css/';
- $this->assertHtml($expected, $result[0]);
- $expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
- $this->assertHtml($expected, $result[1]);
- $this->assertCount(2, $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', ['block' => true]);
- $this->assertNull($result);
- $result = $this->Html->css('more_css_in_head', ['block' => true]);
- $this->assertNull($result);
- $result = $this->Html->css('import-screen', ['rel' => 'import']);
- $expected = [
- '<style',
- 'preg:/@import url\(.*css\/import-screen\.css\);/',
- '/style',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test css() with once option.
- *
- * @return void
- */
- public function testCssLinkOnce()
- {
- $result = $this->Html->css('screen', ['once' => true]);
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/'],
- ];
- $this->assertHtml($expected, $result);
- // Default is once=true
- $result = $this->Html->css('screen');
- $this->assertEquals('', $result);
- $result = $this->Html->css('screen', ['once' => false]);
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/screen\.css/'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCssWithFullBase method
- *
- * @return void
- */
- public function testCssWithFullBase()
- {
- Configure::write('Asset.filter.css', false);
- $here = $this->Html->Url->build('/', true);
- $result = $this->Html->css('screen', ['fullBase' => true]);
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => $here . 'css/screen.css'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPluginCssLink method
- *
- * @return void
- */
- public function testPluginCssLink()
- {
- $this->loadPlugins(['TestPlugin']);
- $result = $this->Html->css('TestPlugin.test_plugin_asset');
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->css('TestPlugin.test_plugin_asset.css', ['once' => false]);
- $this->assertHtml($expected, $result);
- $result = $this->Html->css('TestPlugin.my.css.library');
- $expected['link']['href'] = 'preg:/.*test_plugin\/css\/my\.css\.library\.css/';
- $this->assertHtml($expected, $result);
- $result = $this->Html->css('TestPlugin.test_plugin_asset.css?1234');
- $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
- $this->assertHtml($expected, $result);
- $result = explode("\n", trim($this->Html->css(
- ['TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'],
- ['once' => false]
- )));
- $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
- $this->assertHtml($expected, $result[0]);
- $expected['link']['href'] = 'preg:/.*test_plugin\/css\/vendor\.generic\.css/';
- $this->assertHtml($expected, $result[1]);
- $this->assertCount(2, $result);
- $this->removePlugins(['TestPlugin']);
- }
- /**
- * test use of css() and timestamping
- *
- * @return void
- */
- public function testCssTimestamping()
- {
- Configure::write('debug', true);
- Configure::write('Asset.timestamp', true);
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => ''],
- ];
- $result = $this->Html->css('cake.generic', ['once' => false]);
- $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
- $this->assertHtml($expected, $result);
- Configure::write('debug', false);
- $result = $this->Html->css('cake.generic', ['once' => false]);
- $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
- $this->assertHtml($expected, $result);
- Configure::write('Asset.timestamp', 'force');
- $result = $this->Html->css('cake.generic', ['once' => false]);
- $expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
- $result = $this->Html->css('cake.generic', ['once' => false]);
- $expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
- $result = $this->Html->css('cake.generic', ['once' => false]);
- $expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
- $this->assertHtml($expected, $result);
- }
- /**
- * test use of css() and timestamping with plugin syntax
- *
- * @return void
- */
- public function testPluginCssTimestamping()
- {
- $this->loadPlugins(['TestPlugin']);
- Configure::write('debug', true);
- Configure::write('Asset.timestamp', true);
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => ''],
- ];
- $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
- $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
- $this->assertHtml($expected, $result);
- Configure::write('debug', false);
- $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
- $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
- $this->assertHtml($expected, $result);
- Configure::write('Asset.timestamp', 'force');
- $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
- $expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
- $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
- $expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/longer/'));
- $result = $this->Html->css('TestPlugin.test_plugin_asset', ['once' => false]);
- $expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
- $this->assertHtml($expected, $result);
- $this->removePlugins(['TestPlugin']);
- }
- /**
- * Resource names must be treated differently for css() and script()
- *
- * @return void
- */
- public function testBufferedCssAndScriptWithIdenticalResourceName()
- {
- $this->View->expects($this->at(0))
- ->method('append')
- ->with('css', $this->stringContains('test.min.css'));
- $this->View->expects($this->at(1))
- ->method('append')
- ->with('script', $this->stringContains('test.min.js'));
- $this->Html->css('test.min', ['block' => true]);
- $this->Html->script('test.min', ['block' => true]);
- }
- /**
- * test timestamp enforcement for script tags.
- *
- * @return void
- */
- public function testScriptTimestamping()
- {
- $this->skipIf(!is_writable(WWW_ROOT . 'js'), 'webroot/js is not Writable, timestamp testing has been skipped.');
- Configure::write('debug', true);
- Configure::write('Asset.timestamp', true);
- touch(WWW_ROOT . 'js/__cake_js_test.js');
- $timestamp = substr(strtotime('now'), 0, 8);
- $result = $this->Html->script('__cake_js_test', ['once' => false]);
- $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
- Configure::write('debug', false);
- Configure::write('Asset.timestamp', 'force');
- $result = $this->Html->script('__cake_js_test', ['once' => false]);
- $this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
- unlink(WWW_ROOT . 'js/__cake_js_test.js');
- Configure::write('Asset.timestamp', false);
- }
- /**
- * test timestamp enforcement for script tags with plugin syntax.
- *
- * @return void
- */
- public function testPluginScriptTimestamping()
- {
- $this->loadPlugins(['TestPlugin']);
- $pluginPath = Plugin::path('TestPlugin');
- $pluginJsPath = $pluginPath . 'webroot/js';
- $this->skipIf(!is_writable($pluginJsPath), $pluginJsPath . ' is not Writable, timestamp testing has been skipped.');
- Configure::write('debug', true);
- Configure::write('Asset.timestamp', true);
- touch($pluginJsPath . DS . '__cake_js_test.js');
- $timestamp = substr(strtotime('now'), 0, 8);
- $result = $this->Html->script('TestPlugin.__cake_js_test', ['once' => false]);
- $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
- Configure::write('debug', false);
- Configure::write('Asset.timestamp', 'force');
- $result = $this->Html->script('TestPlugin.__cake_js_test', ['once' => false]);
- $this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
- unlink($pluginJsPath . DS . '__cake_js_test.js');
- Configure::write('Asset.timestamp', false);
- $this->removePlugins(['TestPlugin']);
- }
- /**
- * test that scripts added with uses() are only ever included once.
- * test script tag generation
- *
- * @return void
- */
- public function testScript()
- {
- $result = $this->Html->script('foo');
- $expected = [
- 'script' => ['src' => 'js/foo.js'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script(['foobar', 'bar']);
- $expected = [
- ['script' => ['src' => 'js/foobar.js']],
- '/script',
- ['script' => ['src' => 'js/bar.js']],
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('jquery-1.3');
- $expected = [
- 'script' => ['src' => 'js/jquery-1.3.js'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('test.json');
- $expected = [
- 'script' => ['src' => 'js/test.json.js'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('http://example.com/test.json');
- $expected = [
- 'script' => ['src' => 'http://example.com/test.json'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('/plugin/js/jquery-1.3.2.js?someparam=foo');
- $expected = [
- 'script' => ['src' => '/plugin/js/jquery-1.3.2.js?someparam=foo'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('test.json.js?foo=bar');
- $expected = [
- 'script' => ['src' => 'js/test.json.js?foo=bar'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('test.json.js?foo=bar&other=test');
- $expected = [
- 'script' => ['src' => 'js/test.json.js?foo=bar&other=test'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('x:"><script>alert(1)</script>');
- $expected = [
- 'script' => ['src' => 'x:"><script>alert(1)</script>'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('foo2', ['pathPrefix' => '/my/custom/path/']);
- $expected = [
- 'script' => ['src' => '/my/custom/path/foo2.js'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('foo3', ['pathPrefix' => 'http://cakephp.org/assets/js/']);
- $expected = [
- 'script' => ['src' => 'http://cakephp.org/assets/js/foo3.js'],
- ];
- $this->assertHtml($expected, $result);
- $previousConfig = Configure::read('App.jsBaseUrl');
- Configure::write('App.jsBaseUrl', '//cdn.cakephp.org/js/');
- $result = $this->Html->script('foo4');
- $expected = [
- 'script' => ['src' => '//cdn.cakephp.org/js/foo4.js'],
- ];
- $this->assertHtml($expected, $result);
- Configure::write('App.jsBaseUrl', $previousConfig);
- $result = $this->Html->script('foo');
- $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
- $result = $this->Html->script(['foo', 'bar', 'baz']);
- $this->assertNotRegExp('/foo.js/', $result);
- $result = $this->Html->script('foo', ['once' => false]);
- $this->assertNotNull($result);
- $result = $this->Html->script('jquery-1.3.2', ['defer' => true, 'encoding' => 'utf-8']);
- $expected = [
- 'script' => ['src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test that plugin scripts added with uses() are only ever included once.
- * test script tag generation with plugin syntax
- *
- * @return void
- */
- public function testPluginScript()
- {
- $this->loadPlugins(['TestPlugin']);
- $result = $this->Html->script('TestPlugin.foo');
- $expected = [
- 'script' => ['src' => 'test_plugin/js/foo.js'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script(['TestPlugin.foobar', 'TestPlugin.bar']);
- $expected = [
- ['script' => ['src' => 'test_plugin/js/foobar.js']],
- '/script',
- ['script' => ['src' => 'test_plugin/js/bar.js']],
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('TestPlugin.jquery-1.3');
- $expected = [
- 'script' => ['src' => 'test_plugin/js/jquery-1.3.js'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('TestPlugin.test.json');
- $expected = [
- 'script' => ['src' => 'test_plugin/js/test.json.js'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
- $expected = [
- 'script' => ['src' => 'test_plugin/jquery-1.3.2.js?someparam=foo'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
- $expected = [
- 'script' => ['src' => 'test_plugin/js/test.json.js?foo=bar'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('TestPlugin.foo');
- $this->assertNull($result, 'Script returned upon duplicate inclusion %s');
- $result = $this->Html->script(['TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz']);
- $this->assertNotRegExp('/test_plugin\/js\/foo.js/', $result);
- $result = $this->Html->script('TestPlugin.foo', ['once' => false]);
- $this->assertNotNull($result);
- $result = $this->Html->script('TestPlugin.jquery-1.3.2', ['defer' => true, 'encoding' => 'utf-8']);
- $expected = [
- 'script' => ['src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8'],
- ];
- $this->assertHtml($expected, $result);
- $this->removePlugins(['TestPlugin']);
- }
- /**
- * test that script() works with blocks.
- *
- * @return void
- */
- public function testScriptWithBlocks()
- {
- $this->View->expects($this->at(0))
- ->method('append')
- ->with('script', $this->matchesRegularExpression('/script_in_head.js/'));
- $this->View->expects($this->at(1))
- ->method('append')
- ->with('headScripts', $this->matchesRegularExpression('/second_script.js/'));
- $result = $this->Html->script('script_in_head', ['block' => true]);
- $this->assertNull($result);
- $result = $this->Html->script('second_script', ['block' => 'headScripts']);
- $this->assertNull($result);
- }
- /**
- * testScriptWithFullBase method
- *
- * @return void
- */
- public function testScriptWithFullBase()
- {
- $here = $this->Html->Url->build('/', true);
- $result = $this->Html->script('foo', ['fullBase' => true]);
- $expected = [
- 'script' => ['src' => $here . 'js/foo.js'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script(['foobar', 'bar'], ['fullBase' => true]);
- $expected = [
- ['script' => ['src' => $here . 'js/foobar.js']],
- '/script',
- ['script' => ['src' => $here . 'js/bar.js']],
- '/script',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test a script file in the webroot/theme dir.
- *
- * @return void
- */
- public function testScriptInTheme()
- {
- $this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
- $testfile = WWW_ROOT . '/test_theme/js/__test_js.js';
- $File = new File($testfile, true);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/'));
- $this->Html->Url->getView()->setTheme('TestTheme');
- $result = $this->Html->script('__test_js.js');
- $expected = [
- 'script' => ['src' => '/test_theme/js/__test_js.js'],
- ];
- $this->assertHtml($expected, $result);
- $File->delete();
- }
- /**
- * test Script block generation
- *
- * @return void
- */
- public function testScriptBlock()
- {
- $result = $this->Html->scriptBlock('window.foo = 2;');
- $expected = [
- '<script',
- 'window.foo = 2;',
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->scriptBlock('window.foo = 2;', ['type' => 'text/x-handlebars-template']);
- $expected = [
- 'script' => ['type' => 'text/x-handlebars-template'],
- 'window.foo = 2;',
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->scriptBlock('window.foo = 2;', ['safe' => false]);
- $expected = [
- '<script',
- 'window.foo = 2;',
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->scriptBlock('window.foo = 2;', ['safe' => true]);
- $expected = [
- '<script',
- $this->cDataStart,
- 'window.foo = 2;',
- $this->cDataEnd,
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $this->View->expects($this->at(0))
- ->method('append')
- ->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
- $this->View->expects($this->at(1))
- ->method('append')
- ->with('scriptTop', $this->stringContains('alert('));
- $result = $this->Html->scriptBlock('window.foo = 2;', ['block' => true]);
- $this->assertNull($result);
- $result = $this->Html->scriptBlock('alert("hi")', ['block' => 'scriptTop']);
- $this->assertNull($result);
- $result = $this->Html->scriptBlock('window.foo = 2;', ['safe' => false, 'encoding' => 'utf-8']);
- $expected = [
- 'script' => ['encoding' => 'utf-8'],
- 'window.foo = 2;',
- '/script',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test script tag output buffering when using scriptStart() and scriptEnd();
- *
- * @return void
- */
- public function testScriptStartAndScriptEnd()
- {
- $result = $this->Html->scriptStart();
- $this->assertNull($result);
- echo 'this is some javascript';
- $result = $this->Html->scriptEnd();
- $expected = [
- '<script',
- 'this is some javascript',
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->scriptStart(['safe' => false]);
- $this->assertNull($result);
- echo 'this is some javascript';
- $result = $this->Html->scriptEnd();
- $expected = [
- '<script',
- 'this is some javascript',
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->scriptStart(['safe' => true]);
- $this->assertNull($result);
- echo 'this is some javascript';
- $result = $this->Html->scriptEnd();
- $expected = [
- '<script',
- $this->cDataStart,
- 'this is some javascript',
- $this->cDataEnd,
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->scriptStart(['safe' => true, 'type' => 'text/x-handlebars-template']);
- $this->assertNull($result);
- echo 'this is some template';
- $result = $this->Html->scriptEnd();
- $expected = [
- 'script' => ['type' => 'text/x-handlebars-template'],
- $this->cDataStart,
- 'this is some template',
- $this->cDataEnd,
- '/script',
- ];
- $this->assertHtml($expected, $result);
- $this->View->expects($this->once())
- ->method('append');
- $result = $this->Html->scriptStart(['safe' => false, 'block' => true]);
- $this->assertNull($result);
- echo 'this is some javascript';
- $result = $this->Html->scriptEnd();
- $this->assertNull($result);
- }
- /**
- * testCharsetTag method
- *
- * @return void
- */
- public function testCharsetTag()
- {
- Configure::write('App.encoding', null);
- $result = $this->Html->charset();
- $expected = ['meta' => ['charset' => 'utf-8']];
- $this->assertHtml($expected, $result);
- Configure::write('App.encoding', 'ISO-8859-1');
- $result = $this->Html->charset();
- $expected = ['meta' => ['charset' => 'iso-8859-1']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->charset('UTF-7');
- $expected = ['meta' => ['charset' => 'UTF-7']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testGetCrumb and addCrumb method
- *
- * @group deprecated
- * @return void
- */
- public function testBreadcrumb()
- {
- $this->deprecated(function () {
- $this->assertNull($this->Html->getCrumbs());
- $this->Html->addCrumb('First', '#first');
- $this->Html->addCrumb('Second', '#second');
- $this->Html->addCrumb('Third', '#third');
- $result = $this->Html->getCrumbs();
- $expected = [
- ['a' => ['href' => '#first']],
- 'First',
- '/a',
- '»',
- ['a' => ['href' => '#second']],
- 'Second',
- '/a',
- '»',
- ['a' => ['href' => '#third']],
- 'Third',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->getCrumbs(' > ');
- $expected = [
- ['a' => ['href' => '#first']],
- 'First',
- '/a',
- ' > ',
- ['a' => ['href' => '#second']],
- 'Second',
- '/a',
- ' > ',
- ['a' => ['href' => '#third']],
- 'Third',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $this->Html->addCrumb('Fourth', null);
- $result = $this->Html->getCrumbs();
- $expected = [
- ['a' => ['href' => '#first']],
- 'First',
- '/a',
- '»',
- ['a' => ['href' => '#second']],
- 'Second',
- '/a',
- '»',
- ['a' => ['href' => '#third']],
- 'Third',
- '/a',
- '»',
- 'Fourth',
- ];
- $this->assertHtml($expected, $result);
- $this->Html->addCrumb('Fifth', [
- 'plugin' => false,
- 'controller' => 'controller',
- 'action' => 'action',
- ]);
- $result = $this->Html->getCrumbs('-', 'Start');
- $expected = [
- ['a' => ['href' => '/']],
- 'Start',
- '/a',
- '-',
- ['a' => ['href' => '#first']],
- 'First',
- '/a',
- '-',
- ['a' => ['href' => '#second']],
- 'Second',
- '/a',
- '-',
- ['a' => ['href' => '#third']],
- 'Third',
- '/a',
- '-',
- 'Fourth',
- '-',
- ['a' => ['href' => '/controller/action']],
- 'Fifth',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- });
- }
- /**
- * Test the array form of $startText
- *
- * @group deprecated
- * @return void
- */
- public function testGetCrumbFirstLink()
- {
- $this->deprecated(function () {
- $result = $this->Html->getCrumbList([], 'Home');
- $expected = [
- '<ul',
- ['li' => ['class' => 'first']],
- ['a' => ['href' => '/']], 'Home', '/a',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- $this->Html->addCrumb('First', '#first');
- $this->Html->addCrumb('Second', '#second');
- $result = $this->Html->getCrumbs(' - ', ['url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false]);
- $expected = [
- ['a' => ['href' => '/home']],
- 'img' => ['src' => '/home.png'],
- '/a',
- ' - ',
- ['a' => ['href' => '#first']],
- 'First',
- '/a',
- ' - ',
- ['a' => ['href' => '#second']],
- 'Second',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- });
- }
- /**
- * testNestedList method
- *
- * @return void
- */
- public function testNestedList()
- {
- $list = [
- 'Item 1',
- 'Item 2' => [
- 'Item 2.1',
- ],
- 'Item 3',
- 'Item 4' => [
- 'Item 4.1',
- 'Item 4.2',
- 'Item 4.3' => [
- 'Item 4.3.1',
- 'Item 4.3.2',
- ],
- ],
- 'Item 5' => [
- 'Item 5.1',
- 'Item 5.2',
- ],
- ];
- $result = $this->Html->nestedList($list);
- $expected = [
- '<ul',
- '<li', 'Item 1', '/li',
- '<li', 'Item 2',
- '<ul', '<li', 'Item 2.1', '/li', '/ul',
- '/li',
- '<li', 'Item 3', '/li',
- '<li', 'Item 4',
- '<ul',
- '<li', 'Item 4.1', '/li',
- '<li', 'Item 4.2', '/li',
- '<li', 'Item 4.3',
- '<ul',
- '<li', 'Item 4.3.1', '/li',
- '<li', 'Item 4.3.2', '/li',
- '/ul',
- '/li',
- '/ul',
- '/li',
- '<li', 'Item 5',
- '<ul',
- '<li', 'Item 5.1', '/li',
- '<li', 'Item 5.2', '/li',
- '/ul',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->nestedList($list);
- $this->assertHtml($expected, $result);
- $result = $this->Html->nestedList($list, ['tag' => 'ol']);
- $expected = [
- '<ol',
- '<li', 'Item 1', '/li',
- '<li', 'Item 2',
- '<ol', '<li', 'Item 2.1', '/li', '/ol',
- '/li',
- '<li', 'Item 3', '/li',
- '<li', 'Item 4',
- '<ol',
- '<li', 'Item 4.1', '/li',
- '<li', 'Item 4.2', '/li',
- '<li', 'Item 4.3',
- '<ol',
- '<li', 'Item 4.3.1', '/li',
- '<li', 'Item 4.3.2', '/li',
- '/ol',
- '/li',
- '/ol',
- '/li',
- '<li', 'Item 5',
- '<ol',
- '<li', 'Item 5.1', '/li',
- '<li', 'Item 5.2', '/li',
- '/ol',
- '/li',
- '/ol',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->nestedList($list, ['tag' => 'ol']);
- $this->assertHtml($expected, $result);
- $result = $this->Html->nestedList($list, ['class' => 'list']);
- $expected = [
- ['ul' => ['class' => 'list']],
- '<li', 'Item 1', '/li',
- '<li', 'Item 2',
- ['ul' => ['class' => 'list']], '<li', 'Item 2.1', '/li', '/ul',
- '/li',
- '<li', 'Item 3', '/li',
- '<li', 'Item 4',
- ['ul' => ['class' => 'list']],
- '<li', 'Item 4.1', '/li',
- '<li', 'Item 4.2', '/li',
- '<li', 'Item 4.3',
- ['ul' => ['class' => 'list']],
- '<li', 'Item 4.3.1', '/li',
- '<li', 'Item 4.3.2', '/li',
- '/ul',
- '/li',
- '/ul',
- '/li',
- '<li', 'Item 5',
- ['ul' => ['class' => 'list']],
- '<li', 'Item 5.1', '/li',
- '<li', 'Item 5.2', '/li',
- '/ul',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->nestedList($list, [], ['class' => 'item']);
- $expected = [
- '<ul',
- ['li' => ['class' => 'item']], 'Item 1', '/li',
- ['li' => ['class' => 'item']], 'Item 2',
- '<ul', ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
- '/li',
- ['li' => ['class' => 'item']], 'Item 3', '/li',
- ['li' => ['class' => 'item']], 'Item 4',
- '<ul',
- ['li' => ['class' => 'item']], 'Item 4.1', '/li',
- ['li' => ['class' => 'item']], 'Item 4.2', '/li',
- ['li' => ['class' => 'item']], 'Item 4.3',
- '<ul',
- ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
- ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
- '/ul',
- '/li',
- '/ul',
- '/li',
- ['li' => ['class' => 'item']], 'Item 5',
- '<ul',
- ['li' => ['class' => 'item']], 'Item 5.1', '/li',
- ['li' => ['class' => 'item']], 'Item 5.2', '/li',
- '/ul',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->nestedList($list, [], ['even' => 'even', 'odd' => 'odd']);
- $expected = [
- '<ul',
- ['li' => ['class' => 'odd']], 'Item 1', '/li',
- ['li' => ['class' => 'even']], 'Item 2',
- '<ul', ['li' => ['class' => 'odd']], 'Item 2.1', '/li', '/ul',
- '/li',
- ['li' => ['class' => 'odd']], 'Item 3', '/li',
- ['li' => ['class' => 'even']], 'Item 4',
- '<ul',
- ['li' => ['class' => 'odd']], 'Item 4.1', '/li',
- ['li' => ['class' => 'even']], 'Item 4.2', '/li',
- ['li' => ['class' => 'odd']], 'Item 4.3',
- '<ul',
- ['li' => ['class' => 'odd']], 'Item 4.3.1', '/li',
- ['li' => ['class' => 'even']], 'Item 4.3.2', '/li',
- '/ul',
- '/li',
- '/ul',
- '/li',
- ['li' => ['class' => 'odd']], 'Item 5',
- '<ul',
- ['li' => ['class' => 'odd']], 'Item 5.1', '/li',
- ['li' => ['class' => 'even']], 'Item 5.2', '/li',
- '/ul',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->nestedList($list, ['class' => 'list'], ['class' => 'item']);
- $expected = [
- ['ul' => ['class' => 'list']],
- ['li' => ['class' => 'item']], 'Item 1', '/li',
- ['li' => ['class' => 'item']], 'Item 2',
- ['ul' => ['class' => 'list']], ['li' => ['class' => 'item']], 'Item 2.1', '/li', '/ul',
- '/li',
- ['li' => ['class' => 'item']], 'Item 3', '/li',
- ['li' => ['class' => 'item']], 'Item 4',
- ['ul' => ['class' => 'list']],
- ['li' => ['class' => 'item']], 'Item 4.1', '/li',
- ['li' => ['class' => 'item']], 'Item 4.2', '/li',
- ['li' => ['class' => 'item']], 'Item 4.3',
- ['ul' => ['class' => 'list']],
- ['li' => ['class' => 'item']], 'Item 4.3.1', '/li',
- ['li' => ['class' => 'item']], 'Item 4.3.2', '/li',
- '/ul',
- '/li',
- '/ul',
- '/li',
- ['li' => ['class' => 'item']], 'Item 5',
- ['ul' => ['class' => 'list']],
- ['li' => ['class' => 'item']], 'Item 5.1', '/li',
- ['li' => ['class' => 'item']], 'Item 5.2', '/li',
- '/ul',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testMeta method
- *
- * @return void
- */
- public function testMeta()
- {
- Router::connect('/:controller', ['action' => 'index']);
- $result = $this->Html->meta('this is an rss feed', ['controller' => 'posts', '_ext' => 'rss']);
- $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('rss', ['controller' => 'posts', '_ext' => 'rss'], ['title' => 'this is an rss feed']);
- $expected = ['link' => ['href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('atom', ['controller' => 'posts', '_ext' => 'xml']);
- $expected = ['link' => ['href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('non-existing');
- $expected = ['<meta'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('non-existing', 'some content');
- $expected = ['meta' => ['name' => 'non-existing', 'content' => 'some content']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('non-existing', '/posts.xpp', ['type' => 'atom']);
- $expected = ['link' => ['href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'non-existing']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('atom', ['controller' => 'posts', '_ext' => 'xml'], ['link' => '/articles.rss']);
- $expected = ['link' => ['href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
- $expected = ['meta' => ['name' => 'keywords', 'content' => 'these, are, some, meta, keywords']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('description', 'this is the meta description');
- $expected = ['meta' => ['name' => 'description', 'content' => 'this is the meta description']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('robots', 'ALL');
- $expected = ['meta' => ['name' => 'robots', 'content' => 'ALL']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('viewport', 'width=device-width');
- $expected = [
- 'meta' => ['name' => 'viewport', 'content' => 'width=device-width'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP']);
- $expected = [
- 'meta' => ['property' => 'og:site_name', 'content' => 'CakePHP'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta(['link' => 'http://example.com/manifest', 'rel' => 'manifest']);
- $expected = [
- 'link' => ['href' => 'http://example.com/manifest', 'rel' => 'manifest'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * @return array
- */
- public function dataMetaLinksProvider()
- {
- return [
- ['canonical', ['controller' => 'posts', 'action' => 'show'], '/posts/show'],
- ['first', ['controller' => 'posts', 'action' => 'index'], '/posts'],
- ['last', ['controller' => 'posts', 'action' => 'index', '?' => ['page' => 10]], '/posts?page=10'],
- ['prev', ['controller' => 'posts', 'action' => 'index', '?' => ['page' => 4]], '/posts?page=4'],
- ['next', ['controller' => 'posts', 'action' => 'index', '?' => ['page' => 6]], '/posts?page=6'],
- ];
- }
- /**
- * test canonical and pagination meta links
- *
- * @param string $type
- * @param array $url
- * @param string $expectedUrl
- * @dataProvider dataMetaLinksProvider
- */
- public function testMetaLinks($type, array $url, $expectedUrl)
- {
- $result = $this->Html->meta($type, $url);
- $expected = ['link' => ['href' => $expectedUrl, 'rel' => $type]];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test generating favicon's with meta()
- *
- * @return void
- */
- public function testMetaIcon()
- {
- $result = $this->Html->meta('icon', 'favicon.ico');
- $expected = [
- 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
- ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('icon');
- $expected = [
- 'link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
- ['link' => ['href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('icon', '/favicon.png?one=two&three=four');
- $url = '/favicon.png?one=two&three=four';
- $expected = [
- 'link' => [
- 'href' => $url,
- 'type' => 'image/x-icon',
- 'rel' => 'icon',
- ],
- [
- 'link' => [
- 'href' => $url,
- 'type' => 'image/x-icon',
- 'rel' => 'shortcut icon',
- ],
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('icon', 'x:"><script>alert(1)</script>');
- $url = 'x:"><script>alert(1)</script>';
- $expected = [
- 'link' => [
- 'href' => $url,
- 'type' => 'image/x-icon',
- 'rel' => 'icon',
- ],
- [
- 'link' => [
- 'href' => $url,
- 'type' => 'image/x-icon',
- 'rel' => 'shortcut icon',
- ],
- ],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
- $result = $this->Html->meta('icon');
- $expected = [
- 'link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
- ['link' => ['href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test generating favicon's with meta() with theme
- *
- * @return void
- */
- public function testMetaIconWithTheme()
- {
- $this->Html->Url->getView()->setTheme('TestTheme');
- $result = $this->Html->meta('icon', 'favicon.ico');
- $expected = [
- 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
- ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->meta('icon');
- $expected = [
- 'link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'],
- ['link' => ['href' => 'preg:/.*test_theme\/favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withAttribute('webroot', '/testing/'));
- $result = $this->Html->meta('icon');
- $expected = [
- 'link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'],
- ['link' => ['href' => '/testing/test_theme/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test the inline and block options for meta()
- *
- * @return void
- */
- public function testMetaWithBlocks()
- {
- $this->View->expects($this->at(0))
- ->method('append')
- ->with('meta', $this->stringContains('robots'));
- $this->View->expects($this->at(1))
- ->method('append')
- ->with('metaTags', $this->stringContains('favicon.ico'));
- $result = $this->Html->meta('robots', 'ALL', ['block' => true]);
- $this->assertNull($result);
- $result = $this->Html->meta('icon', 'favicon.ico', ['block' => 'metaTags']);
- $this->assertNull($result);
- }
- /**
- * Test meta() with custom tag and block argument
- */
- public function testMetaCustomWithBlock()
- {
- $this->View->expects($this->at(0))
- ->method('append')
- ->with('meta', $this->stringContains('og:site_name'));
- $this->View->expects($this->at(1))
- ->method('append')
- ->with('meta', $this->stringContains('og:description'));
- $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP', 'block' => true]);
- $this->assertNull($result, 'compact style should work');
- $result = $this->Html->meta(['property' => 'og:description', 'content' => 'CakePHP'], null, ['block' => true]);
- $this->assertNull($result, 'backwards compat style should work.');
- }
- /**
- * testTableHeaders method
- *
- * @return void
- */
- public function testTableHeaders()
- {
- $result = $this->Html->tableHeaders(['ID', 'Name', 'Date']);
- $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight']], 'Date']);
- $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->tableHeaders(['ID', ['Name' => ['class' => 'highlight', 'width' => '120px']], 'Date']);
- $expected = ['<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->tableHeaders(['ID', ['Name' => []], 'Date']);
- $expected = ['<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr'];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTableCells method
- *
- * @return void
- */
- public function testTableCells()
- {
- $tr = [
- 'td content 1',
- ['td content 2', ['width' => '100px']],
- ['td content 3', ['width' => '100px']],
- ];
- $result = $this->Html->tableCells($tr);
- $expected = [
- '<tr',
- '<td', 'td content 1', '/td',
- ['td' => ['width' => '100px']], 'td content 2', '/td',
- ['td' => ['width' => 'preg:/100px/']], 'td content 3', '/td',
- '/tr',
- ];
- $this->assertHtml($expected, $result);
- $tr = ['td content 1', 'td content 2', 'td content 3'];
- $result = $this->Html->tableCells($tr, null, null, true);
- $expected = [
- '<tr',
- ['td' => ['class' => 'column-1']], 'td content 1', '/td',
- ['td' => ['class' => 'column-2']], 'td content 2', '/td',
- ['td' => ['class' => 'column-3']], 'td content 3', '/td',
- '/tr',
- ];
- $this->assertHtml($expected, $result);
- $tr = ['td content 1', 'td content 2', 'td content 3'];
- $result = $this->Html->tableCells($tr, true);
- $expected = [
- '<tr',
- ['td' => ['class' => 'column-1']], 'td content 1', '/td',
- ['td' => ['class' => 'column-2']], 'td content 2', '/td',
- ['td' => ['class' => 'column-3']], 'td content 3', '/td',
- '/tr',
- ];
- $this->assertHtml($expected, $result);
- $tr = [
- ['td content 1', 'td content 2', 'td content 3'],
- ['td content 1', 'td content 2', 'td content 3'],
- ['td content 1', 'td content 2', 'td content 3'],
- ];
- $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
- $expected = "<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
- $this->assertEquals($expected, $result);
- $tr = [
- ['td content 1', 'td content 2', 'td content 3'],
- ['td content 1', 'td content 2', 'td content 3'],
- ['td content 1', 'td content 2', 'td content 3'],
- ['td content 1', 'td content 2', 'td content 3'],
- ];
- $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
- $expected = "<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
- $this->assertEquals($expected, $result);
- $tr = [
- ['td content 1', 'td content 2', 'td content 3'],
- ['td content 1', 'td content 2', 'td content 3'],
- ['td content 1', 'td content 2', 'td content 3'],
- ];
- $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even']);
- $result = $this->Html->tableCells($tr, ['class' => 'odd'], ['class' => 'even'], false, false);
- $expected = "<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
- $this->assertEquals($expected, $result);
- $tr = [
- 'td content 1',
- 'td content 2',
- ['td content 3', ['class' => 'foo']],
- ];
- $result = $this->Html->tableCells($tr, null, null, true);
- $expected = [
- '<tr',
- ['td' => ['class' => 'column-1']], 'td content 1', '/td',
- ['td' => ['class' => 'column-2']], 'td content 2', '/td',
- ['td' => ['class' => 'foo column-3']], 'td content 3', '/td',
- '/tr',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTag method
- *
- * @return void
- */
- public function testTag()
- {
- $result = $this->Html->tag('div');
- $this->assertHtml('<div', $result);
- $result = $this->Html->tag('div', 'text');
- $this->assertHtml(['<div', 'text', '/div'], $result);
- $result = $this->Html->tag('div', '<text>', ['class' => 'class-name', 'escape' => true]);
- $expected = ['div' => ['class' => 'class-name'], '<text>', '/div'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->tag(false, '<em>stuff</em>');
- $this->assertEquals('<em>stuff</em>', $result);
- $result = $this->Html->tag(null, '<em>stuff</em>');
- $this->assertEquals('<em>stuff</em>', $result);
- $result = $this->Html->tag('', '<em>stuff</em>');
- $this->assertEquals('<em>stuff</em>', $result);
- }
- /**
- * testDiv method
- *
- * @return void
- */
- public function testDiv()
- {
- $result = $this->Html->div('class-name');
- $expected = ['div' => ['class' => 'class-name']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->div('class-name', 'text');
- $expected = ['div' => ['class' => 'class-name'], 'text', '/div'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->div('class-name', '<text>', ['escape' => true]);
- $expected = ['div' => ['class' => 'class-name'], '<text>', '/div'];
- $this->assertHtml($expected, $result);
- $evilKey = "><script>alert(1)</script>";
- $options = [$evilKey => 'some value'];
- $result = $this->Html->div('class-name', '', $options);
- $expected = '<div ><script>alert(1)</script>="some value" class="class-name"></div>';
- $this->assertEquals($expected, $result);
- }
- /**
- * testPara method
- *
- * @return void
- */
- public function testPara()
- {
- $result = $this->Html->para('class-name', '');
- $expected = ['p' => ['class' => 'class-name']];
- $this->assertHtml($expected, $result);
- $result = $this->Html->para('class-name', 'text');
- $expected = ['p' => ['class' => 'class-name'], 'text', '/p'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->para('class-name', '<text>', ['escape' => true]);
- $expected = ['p' => ['class' => 'class-name'], '<text>', '/p'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->para('class-name', 'text"', ['escape' => false]);
- $expected = ['p' => ['class' => 'class-name'], 'text"', '/p'];
- $this->assertHtml($expected, $result);
- }
- /**
- * testMedia method
- *
- * @return void
- */
- public function testMedia()
- {
- $result = $this->Html->media('video.webm');
- $expected = ['video' => ['src' => 'files/video.webm'], '/video'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->media('video.webm', [
- 'text' => 'Your browser does not support the HTML5 Video element.',
- ]);
- $expected = ['video' => ['src' => 'files/video.webm'], 'Your browser does not support the HTML5 Video element.', '/video'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->media('video.webm', ['autoload', 'muted' => 'muted']);
- $expected = [
- 'video' => [
- 'src' => 'files/video.webm',
- 'autoload' => 'autoload',
- 'muted' => 'muted',
- ],
- '/video',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->media(
- ['video.webm', ['src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'"]],
- ['pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.']
- );
- $expected = [
- 'video' => ['poster' => Configure::read('App.imageBaseUrl') . 'poster.jpg'],
- ['source' => ['src' => 'videos/video.webm', 'type' => 'video/webm']],
- ['source' => ['src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs='theora, vorbis'']],
- 'Your browser does not support the HTML5 Video element.',
- '/video',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->media('video.ogv', ['tag' => 'video']);
- $expected = ['video' => ['src' => 'files/video.ogv'], '/video'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->media('audio.mp3');
- $expected = ['audio' => ['src' => 'files/audio.mp3'], '/audio'];
- $this->assertHtml($expected, $result);
- $result = $this->Html->media(
- [['src' => 'video.mov', 'type' => 'video/mp4'], 'video.webm']
- );
- $expected = [
- '<video',
- ['source' => ['src' => 'files/video.mov', 'type' => 'video/mp4']],
- ['source' => ['src' => 'files/video.webm', 'type' => 'video/webm']],
- '/video',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->media(null, ['src' => 'video.webm']);
- $expected = [
- 'video' => ['src' => 'files/video.webm'],
- '/video',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCrumbList method
- *
- * @group deprecated
- * @return void
- */
- public function testCrumbList()
- {
- $this->deprecated(function () {
- $this->assertNull($this->Html->getCrumbList());
- $this->Html->addCrumb('Home', '/', ['class' => 'home']);
- $this->Html->addCrumb('Some page', '/some_page');
- $this->Html->addCrumb('Another page');
- $result = $this->Html->getCrumbList(
- ['class' => 'breadcrumbs']
- );
- $expected = [
- ['ul' => ['class' => 'breadcrumbs']],
- ['li' => ['class' => 'first']],
- ['a' => ['class' => 'home', 'href' => '/']], 'Home', '/a',
- '/li',
- '<li',
- ['a' => ['href' => '/some_page']], 'Some page', '/a',
- '/li',
- ['li' => ['class' => 'last']],
- 'Another page',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- });
- }
- /**
- * Test getCrumbList startText
- *
- * @group deprecated
- * @return void
- */
- public function testCrumbListFirstLink()
- {
- $this->deprecated(function () {
- $this->Html->addCrumb('First', '#first');
- $this->Html->addCrumb('Second', '#second');
- $result = $this->Html->getCrumbList([], 'Home');
- $expected = [
- '<ul',
- ['li' => ['class' => 'first']],
- ['a' => ['href' => '/']], 'Home', '/a',
- '/li',
- '<li',
- ['a' => ['href' => '#first']], 'First', '/a',
- '/li',
- ['li' => ['class' => 'last']],
- ['a' => ['href' => '#second']], 'Second', '/a',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->getCrumbList([], ['url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false]);
- $expected = [
- '<ul',
- ['li' => ['class' => 'first']],
- ['a' => ['href' => '/home']], 'img' => ['src' => '/home.png'], '/a',
- '/li',
- '<li',
- ['a' => ['href' => '#first']], 'First', '/a',
- '/li',
- ['li' => ['class' => 'last']],
- ['a' => ['href' => '#second']], 'Second', '/a',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- });
- }
- /**
- * test getCrumbList() in Twitter Bootstrap style.
- *
- * @group deprecated
- * @return void
- */
- public function testCrumbListBootstrapStyle()
- {
- $this->deprecated(function () {
- $this->Html->addCrumb('Home', '/', ['class' => 'home']);
- $this->Html->addCrumb('Library', '/lib');
- $this->Html->addCrumb('Data');
- $result = $this->Html->getCrumbList([
- 'class' => 'breadcrumb',
- 'separator' => '<span class="divider">-</span>',
- 'firstClass' => false,
- 'lastClass' => 'active',
- ]);
- $expected = [
- ['ul' => ['class' => 'breadcrumb']],
- '<li',
- ['a' => ['class' => 'home', 'href' => '/']], 'Home', '/a',
- ['span' => ['class' => 'divider']], '-', '/span',
- '/li',
- '<li',
- ['a' => ['href' => '/lib']], 'Library', '/a',
- ['span' => ['class' => 'divider']], '-', '/span',
- '/li',
- ['li' => ['class' => 'active']], 'Data', '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result);
- });
- }
- /**
- * Test GetCrumbList using style of Zurb Foundation.
- *
- * @group deprecated
- * @return void
- */
- public function testCrumbListZurbStyle()
- {
- $this->deprecated(function () {
- $this->Html->addCrumb('Home', '#');
- $this->Html->addCrumb('Features', '#');
- $this->Html->addCrumb('Gene Splicing', '#');
- $this->Html->addCrumb('Home', '#');
- $result = $this->Html->getCrumbList(
- ['class' => 'breadcrumbs', 'firstClass' => false, 'lastClass' => 'current']
- );
- $expected = [
- ['ul' => ['class' => 'breadcrumbs']],
- '<li',
- ['a' => ['href' => '#']], 'Home', '/a',
- '/li',
- '<li',
- ['a' => ['href' => '#']], 'Features', '/a',
- '/li',
- '<li',
- ['a' => ['href' => '#']], 'Gene Splicing', '/a',
- '/li',
- ['li' => ['class' => 'current']],
- ['a' => ['href' => '#']], 'Home', '/a',
- '/li',
- '/ul',
- ];
- $this->assertHtml($expected, $result, true);
- });
- }
- /**
- * Tests that CSS and Javascript files of the same name don't conflict with the 'once' test
- *
- * @return void
- */
- public function testCssAndScriptWithSameName()
- {
- $result = $this->Html->css('foo');
- $expected = [
- 'link' => ['rel' => 'stylesheet', 'href' => 'preg:/.*css\/foo\.css/'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Html->script('foo');
- $expected = [
- 'script' => ['src' => 'js/foo.js'],
- ];
- $this->assertHtml($expected, $result);
- }
- }
|