HeadScriptTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_View_Helper_HeadScriptTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadScriptTest::main");
  25. }
  26. /** Zend_View_Helper_HeadScript */
  27. require_once 'Zend/View/Helper/HeadScript.php';
  28. /** Zend_View_Helper_Placeholder_Registry */
  29. require_once 'Zend/View/Helper/Placeholder/Registry.php';
  30. /** Zend_Registry */
  31. require_once 'Zend/Registry.php';
  32. /**
  33. * Test class for Zend_View_Helper_HeadScript.
  34. *
  35. * @category Zend
  36. * @package Zend_View
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_View
  41. * @group Zend_View_Helper
  42. */
  43. class Zend_View_Helper_HeadScriptTest extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * @var Zend_View_Helper_HeadScript
  47. */
  48. public $helper;
  49. /**
  50. * @var string
  51. */
  52. public $basePath;
  53. /**
  54. * Runs the test methods of this class.
  55. *
  56. * @return void
  57. */
  58. public static function main()
  59. {
  60. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadScriptTest");
  61. $result = PHPUnit_TextUI_TestRunner::run($suite);
  62. }
  63. /**
  64. * Sets up the fixture, for example, open a network connection.
  65. * This method is called before a test is executed.
  66. *
  67. * @return void
  68. */
  69. public function setUp()
  70. {
  71. $regKey = Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY;
  72. if (Zend_Registry::isRegistered($regKey)) {
  73. $registry = Zend_Registry::getInstance();
  74. unset($registry[$regKey]);
  75. }
  76. $this->basePath = dirname(__FILE__) . '/_files/modules';
  77. $this->helper = new Zend_View_Helper_HeadScript();
  78. }
  79. /**
  80. * Tears down the fixture, for example, close a network connection.
  81. * This method is called after a test is executed.
  82. *
  83. * @return void
  84. */
  85. public function tearDown()
  86. {
  87. unset($this->helper);
  88. }
  89. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  90. {
  91. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  92. if ($registry->containerExists('Zend_View_Helper_HeadScript')) {
  93. $registry->deleteContainer('Zend_View_Helper_HeadScript');
  94. }
  95. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadScript'));
  96. $helper = new Zend_View_Helper_HeadScript();
  97. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadScript'));
  98. }
  99. public function testHeadScriptReturnsObjectInstance()
  100. {
  101. $placeholder = $this->helper->headScript();
  102. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadScript);
  103. }
  104. public function testSetPrependAppendAndOffsetSetThrowExceptionsOnInvalidItems()
  105. {
  106. try {
  107. $this->helper->append('foo');
  108. $this->fail('Append should throw exception with invalid item');
  109. } catch (Zend_View_Exception $e) { }
  110. try {
  111. $this->helper->offsetSet(1, 'foo');
  112. $this->fail('OffsetSet should throw exception with invalid item');
  113. } catch (Zend_View_Exception $e) { }
  114. try {
  115. $this->helper->prepend('foo');
  116. $this->fail('Prepend should throw exception with invalid item');
  117. } catch (Zend_View_Exception $e) { }
  118. try {
  119. $this->helper->set('foo');
  120. $this->fail('Set should throw exception with invalid item');
  121. } catch (Zend_View_Exception $e) { }
  122. }
  123. protected function _inflectAction($type)
  124. {
  125. return ucfirst(strtolower($type));
  126. }
  127. protected function _testOverloadAppend($type)
  128. {
  129. $action = 'append' . $this->_inflectAction($type);
  130. $string = 'foo';
  131. for ($i = 0; $i < 3; ++$i) {
  132. $string .= ' foo';
  133. $this->helper->$action($string);
  134. $values = $this->helper->getArrayCopy();
  135. $this->assertEquals($i + 1, count($values));
  136. if ('file' == $type) {
  137. $this->assertEquals($string, $values[$i]->attributes['src']);
  138. } elseif ('script' == $type) {
  139. $this->assertEquals($string, $values[$i]->source);
  140. }
  141. $this->assertEquals('text/javascript', $values[$i]->type);
  142. }
  143. }
  144. protected function _testOverloadPrepend($type)
  145. {
  146. $action = 'prepend' . $this->_inflectAction($type);
  147. $string = 'foo';
  148. for ($i = 0; $i < 3; ++$i) {
  149. $string .= ' foo';
  150. $this->helper->$action($string);
  151. $values = $this->helper->getArrayCopy();
  152. $this->assertEquals($i + 1, count($values));
  153. $first = array_shift($values);
  154. if ('file' == $type) {
  155. $this->assertEquals($string, $first->attributes['src']);
  156. } elseif ('script' == $type) {
  157. $this->assertEquals($string, $first->source);
  158. }
  159. $this->assertEquals('text/javascript', $first->type);
  160. }
  161. }
  162. protected function _testOverloadSet($type)
  163. {
  164. $action = 'set' . $this->_inflectAction($type);
  165. $string = 'foo';
  166. for ($i = 0; $i < 3; ++$i) {
  167. $this->helper->appendScript($string);
  168. $string .= ' foo';
  169. }
  170. $this->helper->$action($string);
  171. $values = $this->helper->getArrayCopy();
  172. $this->assertEquals(1, count($values));
  173. if ('file' == $type) {
  174. $this->assertEquals($string, $values[0]->attributes['src']);
  175. } elseif ('script' == $type) {
  176. $this->assertEquals($string, $values[0]->source);
  177. }
  178. $this->assertEquals('text/javascript', $values[0]->type);
  179. }
  180. protected function _testOverloadOffsetSet($type)
  181. {
  182. $action = 'offsetSet' . $this->_inflectAction($type);
  183. $string = 'foo';
  184. $this->helper->$action(5, $string);
  185. $values = $this->helper->getArrayCopy();
  186. $this->assertEquals(1, count($values));
  187. if ('file' == $type) {
  188. $this->assertEquals($string, $values[5]->attributes['src']);
  189. } elseif ('script' == $type) {
  190. $this->assertEquals($string, $values[5]->source);
  191. }
  192. $this->assertEquals('text/javascript', $values[5]->type);
  193. }
  194. public function testOverloadAppendFileAppendsScriptsToStack()
  195. {
  196. $this->_testOverloadAppend('file');
  197. }
  198. public function testOverloadAppendScriptAppendsScriptsToStack()
  199. {
  200. $this->_testOverloadAppend('script');
  201. }
  202. public function testOverloadPrependFileAppendsScriptsToStack()
  203. {
  204. $this->_testOverloadPrepend('file');
  205. }
  206. public function testOverloadPrependScriptAppendsScriptsToStack()
  207. {
  208. $this->_testOverloadPrepend('script');
  209. }
  210. public function testOverloadSetFileOverwritesStack()
  211. {
  212. $this->_testOverloadSet('file');
  213. }
  214. public function testOverloadSetScriptOverwritesStack()
  215. {
  216. $this->_testOverloadSet('script');
  217. }
  218. public function testOverloadOffsetSetFileWritesToSpecifiedIndex()
  219. {
  220. $this->_testOverloadOffsetSet('file');
  221. }
  222. public function testOverloadOffsetSetScriptWritesToSpecifiedIndex()
  223. {
  224. $this->_testOverloadOffsetSet('script');
  225. }
  226. public function testOverloadingThrowsExceptionWithInvalidMethod()
  227. {
  228. try {
  229. $this->helper->fooBar('foo');
  230. $this->fail('Invalid method should raise exception');
  231. } catch (Zend_View_Exception $e) {
  232. }
  233. }
  234. public function testOverloadingWithTooFewArgumentsRaisesException()
  235. {
  236. try {
  237. $this->helper->setScript();
  238. $this->fail('Too few arguments should raise exception');
  239. } catch (Zend_View_Exception $e) {
  240. }
  241. try {
  242. $this->helper->offsetSetScript(5);
  243. $this->fail('Too few arguments should raise exception');
  244. } catch (Zend_View_Exception $e) {
  245. }
  246. }
  247. public function testHeadScriptAppropriatelySetsScriptItems()
  248. {
  249. $this->helper->headScript('FILE', 'foo', 'set')
  250. ->headScript('SCRIPT', 'bar', 'prepend')
  251. ->headScript('SCRIPT', 'baz', 'append');
  252. $items = $this->helper->getArrayCopy();
  253. for ($i = 0; $i < 3; ++$i) {
  254. $item = $items[$i];
  255. switch ($i) {
  256. case 0:
  257. $this->assertObjectHasAttribute('source', $item);
  258. $this->assertEquals('bar', $item->source);
  259. break;
  260. case 1:
  261. $this->assertObjectHasAttribute('attributes', $item);
  262. $this->assertTrue(isset($item->attributes['src']));
  263. $this->assertEquals('foo', $item->attributes['src']);
  264. break;
  265. case 2:
  266. $this->assertObjectHasAttribute('source', $item);
  267. $this->assertEquals('baz', $item->source);
  268. break;
  269. }
  270. }
  271. }
  272. public function testToStringRendersValidHtml()
  273. {
  274. $this->helper->headScript('FILE', 'foo', 'set')
  275. ->headScript('SCRIPT', 'bar', 'prepend')
  276. ->headScript('SCRIPT', 'baz', 'append');
  277. $string = $this->helper->toString();
  278. $scripts = substr_count($string, '<script ');
  279. $this->assertEquals(3, $scripts);
  280. $scripts = substr_count($string, '</script>');
  281. $this->assertEquals(3, $scripts);
  282. $scripts = substr_count($string, 'src="');
  283. $this->assertEquals(1, $scripts);
  284. $scripts = substr_count($string, '><');
  285. $this->assertEquals(1, $scripts);
  286. $this->assertContains('src="foo"', $string);
  287. $this->assertContains('bar', $string);
  288. $this->assertContains('baz', $string);
  289. $doc = new DOMDocument;
  290. $dom = $doc->loadHtml($string);
  291. $this->assertTrue($dom !== false);
  292. }
  293. public function testCapturingCapturesToObject()
  294. {
  295. $this->helper->captureStart();
  296. echo 'foobar';
  297. $this->helper->captureEnd();
  298. $values = $this->helper->getArrayCopy();
  299. $this->assertEquals(1, count($values), var_export($values, 1));
  300. $item = array_shift($values);
  301. $this->assertContains('foobar', $item->source);
  302. }
  303. public function testIndentationIsHonored()
  304. {
  305. $this->helper->setIndent(4);
  306. $this->helper->appendScript('
  307. var foo = "bar";
  308. document.write(foo.strlen());');
  309. $this->helper->appendScript('
  310. var bar = "baz";
  311. document.write(bar.strlen());');
  312. $string = $this->helper->toString();
  313. $scripts = substr_count($string, ' <script');
  314. $this->assertEquals(2, $scripts);
  315. $this->assertContains(' //', $string);
  316. $this->assertContains('var', $string);
  317. $this->assertContains('document', $string);
  318. $this->assertContains(' document', $string);
  319. }
  320. public function testDoesNotAllowDuplicateFiles()
  321. {
  322. $this->helper->headScript('FILE', '/js/prototype.js');
  323. $this->helper->headScript('FILE', '/js/prototype.js');
  324. $this->assertEquals(1, count($this->helper));
  325. }
  326. public function testRenderingDoesNotRenderArbitraryAttributesByDefault()
  327. {
  328. $this->helper->headScript()->appendFile('/js/foo.js', 'text/javascript', array('bogus' => 'deferred'));
  329. $test = $this->helper->headScript()->toString();
  330. $this->assertNotContains('bogus="deferred"', $test);
  331. }
  332. public function testCanRenderArbitraryAttributesOnRequest()
  333. {
  334. $this->helper->headScript()->appendFile('/js/foo.js', 'text/javascript', array('bogus' => 'deferred'))
  335. ->setAllowArbitraryAttributes(true);
  336. $test = $this->helper->headScript()->toString();
  337. $this->assertContains('bogus="deferred"', $test);
  338. }
  339. public function testCanPerformMultipleSerialCaptures()
  340. {
  341. $this->helper->headScript()->captureStart();
  342. echo "this is something captured";
  343. $this->helper->headScript()->captureEnd();
  344. try {
  345. $this->helper->headScript()->captureStart();
  346. } catch (Zend_View_Exception $e) {
  347. $this->fail('Serial captures should be allowed');
  348. }
  349. echo "this is something else captured";
  350. $this->helper->headScript()->captureEnd();
  351. }
  352. public function testCannotNestCaptures()
  353. {
  354. $this->helper->headScript()->captureStart();
  355. echo "this is something captured";
  356. try {
  357. $this->helper->headScript()->captureStart();
  358. $this->helper->headScript()->captureEnd();
  359. $this->fail('Should not be able to nest captures');
  360. } catch (Zend_View_Exception $e) {
  361. $this->helper->headScript()->captureEnd();
  362. $this->assertContains('Cannot nest', $e->getMessage());
  363. }
  364. }
  365. /**
  366. * @group ZF-3928
  367. * @link http://framework.zend.com/issues/browse/ZF-3928
  368. */
  369. public function testTurnOffAutoEscapeDoesNotEncodeAmpersand()
  370. {
  371. $this->helper->setAutoEscape(false)->appendFile('test.js?id=123&foo=bar');
  372. $this->assertEquals('<script type="text/javascript" src="test.js?id=123&foo=bar"></script>', $this->helper->toString());
  373. }
  374. public function testConditionalScript()
  375. {
  376. $this->helper->headScript()->appendFile('/js/foo.js', 'text/javascript', array('conditional' => 'lt IE 7'));
  377. $test = $this->helper->headScript()->toString();
  378. $this->assertContains('<!--[if lt IE 7]>', $test);
  379. }
  380. public function testConditionalScriptWidthIndentation()
  381. {
  382. $this->helper->headScript()->appendFile('/js/foo.js', 'text/javascript', array('conditional' => 'lt IE 7'));
  383. $this->helper->headScript()->setIndent(4);
  384. $test = $this->helper->headScript()->toString();
  385. $this->assertContains(' <!--[if lt IE 7]>', $test);
  386. }
  387. /**
  388. * @group ZF-5435
  389. */
  390. public function testContainerMaintainsCorrectOrderOfItems()
  391. {
  392. $this->helper->offsetSetFile(1, 'test1.js');
  393. $this->helper->offsetSetFile(20, 'test2.js');
  394. $this->helper->offsetSetFile(10, 'test3.js');
  395. $this->helper->offsetSetFile(5, 'test4.js');
  396. $test = $this->helper->toString();
  397. $expected = '<script type="text/javascript" src="test1.js"></script>' . PHP_EOL
  398. . '<script type="text/javascript" src="test4.js"></script>' . PHP_EOL
  399. . '<script type="text/javascript" src="test3.js"></script>' . PHP_EOL
  400. . '<script type="text/javascript" src="test2.js"></script>';
  401. $this->assertEquals($expected, $test);
  402. }
  403. /**
  404. * @group ZF-12048
  405. */
  406. public function testSetFileStillOverwritesExistingFilesWhenItsADuplicate()
  407. {
  408. $this->helper->appendFile('foo.js');
  409. $this->helper->appendFile('bar.js');
  410. $this->helper->setFile('foo.js');
  411. $expected = '<script type="text/javascript" src="foo.js"></script>';
  412. $test = $this->helper->toString();
  413. $this->assertEquals($expected, $test);
  414. }
  415. /**
  416. * @group ZF-12287
  417. */
  418. public function testConditionalWithAllowArbitraryAttributesDoesNotIncludeConditionalScript()
  419. {
  420. $this->helper->setAllowArbitraryAttributes(true);
  421. $this->helper->appendFile(
  422. '/js/foo.js', 'text/javascript', array('conditional' => 'lt IE 7')
  423. );
  424. $test = $this->helper->toString();
  425. $this->assertNotContains('conditional', $test);
  426. }
  427. /**
  428. * @group ZF-12287
  429. */
  430. public function testNoEscapeWithAllowArbitraryAttributesDoesNotIncludeNoEscapeScript()
  431. {
  432. $this->helper->setAllowArbitraryAttributes(true);
  433. $this->helper->appendScript(
  434. '// some script', 'text/javascript', array('noescape' => true)
  435. );
  436. $test = $this->helper->toString();
  437. $this->assertNotContains('noescape', $test);
  438. }
  439. /**
  440. * @group ZF-12287
  441. */
  442. public function testNoEscapeDefaultsToFalse()
  443. {
  444. $this->helper->appendScript(
  445. '// some script' . PHP_EOL, 'text/javascript', array()
  446. );
  447. $test = $this->helper->toString();
  448. $this->assertContains('//<!--', $test);
  449. $this->assertContains('//-->', $test);
  450. }
  451. /**
  452. * @group ZF-12287
  453. */
  454. public function testNoEscapeTrue()
  455. {
  456. $this->helper->appendScript(
  457. '// some script' . PHP_EOL, 'text/javascript', array('noescape' => true)
  458. );
  459. $test = $this->helper->toString();
  460. $this->assertNotContains('//<!--', $test);
  461. $this->assertNotContains('//-->', $test);
  462. }
  463. /**
  464. * @group GH-515
  465. */
  466. public function testConditionalScriptNoIE()
  467. {
  468. $this->helper->setAllowArbitraryAttributes(true);
  469. $this->helper->appendFile(
  470. '/js/foo.js', 'text/javascript', array('conditional' => '!IE')
  471. );
  472. $test = $this->helper->toString();
  473. $this->assertContains('<!--[if !IE]><!--><', $test);
  474. $this->assertContains('<!--<![endif]-->', $test);
  475. }
  476. }
  477. // Call Zend_View_Helper_HeadScriptTest::main() if this source file is executed directly.
  478. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadScriptTest::main") {
  479. Zend_View_Helper_HeadScriptTest::main();
  480. }