2
0

HeadScriptTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. // Call Zend_View_Helper_HeadScriptTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadScriptTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. /** Zend_View_Helper_HeadScript */
  8. require_once 'Zend/View/Helper/HeadScript.php';
  9. /** Zend_View_Helper_Placeholder_Registry */
  10. require_once 'Zend/View/Helper/Placeholder/Registry.php';
  11. /** Zend_Registry */
  12. require_once 'Zend/Registry.php';
  13. /**
  14. * Test class for Zend_View_Helper_HeadScript.
  15. *
  16. * @category Zend
  17. * @package Zend_View
  18. * @subpackage UnitTests
  19. */
  20. class Zend_View_Helper_HeadScriptTest extends PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * @var Zend_View_Helper_HeadScript
  24. */
  25. public $helper;
  26. /**
  27. * @var string
  28. */
  29. public $basePath;
  30. /**
  31. * Runs the test methods of this class.
  32. *
  33. * @return void
  34. */
  35. public static function main()
  36. {
  37. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadScriptTest");
  38. $result = PHPUnit_TextUI_TestRunner::run($suite);
  39. }
  40. /**
  41. * Sets up the fixture, for example, open a network connection.
  42. * This method is called before a test is executed.
  43. *
  44. * @return void
  45. */
  46. public function setUp()
  47. {
  48. $regKey = Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY;
  49. if (Zend_Registry::isRegistered($regKey)) {
  50. $registry = Zend_Registry::getInstance();
  51. unset($registry[$regKey]);
  52. }
  53. $this->basePath = dirname(__FILE__) . '/_files/modules';
  54. $this->helper = new Zend_View_Helper_HeadScript();
  55. }
  56. /**
  57. * Tears down the fixture, for example, close a network connection.
  58. * This method is called after a test is executed.
  59. *
  60. * @return void
  61. */
  62. public function tearDown()
  63. {
  64. unset($this->helper);
  65. }
  66. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  67. {
  68. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  69. if ($registry->containerExists('Zend_View_Helper_HeadScript')) {
  70. $registry->deleteContainer('Zend_View_Helper_HeadScript');
  71. }
  72. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadScript'));
  73. $helper = new Zend_View_Helper_HeadScript();
  74. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadScript'));
  75. }
  76. public function testHeadScriptReturnsObjectInstance()
  77. {
  78. $placeholder = $this->helper->headScript();
  79. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadScript);
  80. }
  81. public function testSetPrependAppendAndOffsetSetThrowExceptionsOnInvalidItems()
  82. {
  83. try {
  84. $this->helper->append('foo');
  85. $this->fail('Append should throw exception with invalid item');
  86. } catch (Zend_View_Exception $e) { }
  87. try {
  88. $this->helper->offsetSet(1, 'foo');
  89. $this->fail('OffsetSet should throw exception with invalid item');
  90. } catch (Zend_View_Exception $e) { }
  91. try {
  92. $this->helper->prepend('foo');
  93. $this->fail('Prepend should throw exception with invalid item');
  94. } catch (Zend_View_Exception $e) { }
  95. try {
  96. $this->helper->set('foo');
  97. $this->fail('Set should throw exception with invalid item');
  98. } catch (Zend_View_Exception $e) { }
  99. }
  100. protected function _inflectAction($type)
  101. {
  102. return ucfirst(strtolower($type));
  103. }
  104. protected function _testOverloadAppend($type)
  105. {
  106. $action = 'append' . $this->_inflectAction($type);
  107. $string = 'foo';
  108. for ($i = 0; $i < 3; ++$i) {
  109. $string .= ' foo';
  110. $this->helper->$action($string);
  111. $values = $this->helper->getArrayCopy();
  112. $this->assertEquals($i + 1, count($values));
  113. if ('file' == $type) {
  114. $this->assertEquals($string, $values[$i]->attributes['src']);
  115. } elseif ('script' == $type) {
  116. $this->assertEquals($string, $values[$i]->source);
  117. }
  118. $this->assertEquals('text/javascript', $values[$i]->type);
  119. }
  120. }
  121. protected function _testOverloadPrepend($type)
  122. {
  123. $action = 'prepend' . $this->_inflectAction($type);
  124. $string = 'foo';
  125. for ($i = 0; $i < 3; ++$i) {
  126. $string .= ' foo';
  127. $this->helper->$action($string);
  128. $values = $this->helper->getArrayCopy();
  129. $this->assertEquals($i + 1, count($values));
  130. $first = array_shift($values);
  131. if ('file' == $type) {
  132. $this->assertEquals($string, $first->attributes['src']);
  133. } elseif ('script' == $type) {
  134. $this->assertEquals($string, $first->source);
  135. }
  136. $this->assertEquals('text/javascript', $first->type);
  137. }
  138. }
  139. protected function _testOverloadSet($type)
  140. {
  141. $action = 'set' . $this->_inflectAction($type);
  142. $string = 'foo';
  143. for ($i = 0; $i < 3; ++$i) {
  144. $this->helper->appendScript($string);
  145. $string .= ' foo';
  146. }
  147. $this->helper->$action($string);
  148. $values = $this->helper->getArrayCopy();
  149. $this->assertEquals(1, count($values));
  150. if ('file' == $type) {
  151. $this->assertEquals($string, $values[0]->attributes['src']);
  152. } elseif ('script' == $type) {
  153. $this->assertEquals($string, $values[0]->source);
  154. }
  155. $this->assertEquals('text/javascript', $values[0]->type);
  156. }
  157. protected function _testOverloadOffsetSet($type)
  158. {
  159. $action = 'offsetSet' . $this->_inflectAction($type);
  160. $string = 'foo';
  161. $this->helper->$action(5, $string);
  162. $values = $this->helper->getArrayCopy();
  163. $this->assertEquals(1, count($values));
  164. if ('file' == $type) {
  165. $this->assertEquals($string, $values[5]->attributes['src']);
  166. } elseif ('script' == $type) {
  167. $this->assertEquals($string, $values[5]->source);
  168. }
  169. $this->assertEquals('text/javascript', $values[5]->type);
  170. }
  171. public function testOverloadAppendFileAppendsScriptsToStack()
  172. {
  173. $this->_testOverloadAppend('file');
  174. }
  175. public function testOverloadAppendScriptAppendsScriptsToStack()
  176. {
  177. $this->_testOverloadAppend('script');
  178. }
  179. public function testOverloadPrependFileAppendsScriptsToStack()
  180. {
  181. $this->_testOverloadPrepend('file');
  182. }
  183. public function testOverloadPrependScriptAppendsScriptsToStack()
  184. {
  185. $this->_testOverloadPrepend('script');
  186. }
  187. public function testOverloadSetFileOverwritesStack()
  188. {
  189. $this->_testOverloadSet('file');
  190. }
  191. public function testOverloadSetScriptOverwritesStack()
  192. {
  193. $this->_testOverloadSet('script');
  194. }
  195. public function testOverloadOffsetSetFileWritesToSpecifiedIndex()
  196. {
  197. $this->_testOverloadOffsetSet('file');
  198. }
  199. public function testOverloadOffsetSetScriptWritesToSpecifiedIndex()
  200. {
  201. $this->_testOverloadOffsetSet('script');
  202. }
  203. public function testOverloadingThrowsExceptionWithInvalidMethod()
  204. {
  205. try {
  206. $this->helper->fooBar('foo');
  207. $this->fail('Invalid method should raise exception');
  208. } catch (Zend_View_Exception $e) {
  209. }
  210. }
  211. public function testOverloadingWithTooFewArgumentsRaisesException()
  212. {
  213. try {
  214. $this->helper->setScript();
  215. $this->fail('Too few arguments should raise exception');
  216. } catch (Zend_View_Exception $e) {
  217. }
  218. try {
  219. $this->helper->offsetSetScript(5);
  220. $this->fail('Too few arguments should raise exception');
  221. } catch (Zend_View_Exception $e) {
  222. }
  223. }
  224. public function testHeadScriptAppropriatelySetsScriptItems()
  225. {
  226. $this->helper->headScript('FILE', 'foo', 'set')
  227. ->headScript('SCRIPT', 'bar', 'prepend')
  228. ->headScript('SCRIPT', 'baz', 'append');
  229. $items = $this->helper->getArrayCopy();
  230. for ($i = 0; $i < 3; ++$i) {
  231. $item = $items[$i];
  232. switch ($i) {
  233. case 0:
  234. $this->assertObjectHasAttribute('source', $item);
  235. $this->assertEquals('bar', $item->source);
  236. break;
  237. case 1:
  238. $this->assertObjectHasAttribute('attributes', $item);
  239. $this->assertTrue(isset($item->attributes['src']));
  240. $this->assertEquals('foo', $item->attributes['src']);
  241. break;
  242. case 2:
  243. $this->assertObjectHasAttribute('source', $item);
  244. $this->assertEquals('baz', $item->source);
  245. break;
  246. }
  247. }
  248. }
  249. public function testToStringRendersValidHtml()
  250. {
  251. $this->helper->headScript('FILE', 'foo', 'set')
  252. ->headScript('SCRIPT', 'bar', 'prepend')
  253. ->headScript('SCRIPT', 'baz', 'append');
  254. $string = $this->helper->toString();
  255. $scripts = substr_count($string, '<script ');
  256. $this->assertEquals(3, $scripts);
  257. $scripts = substr_count($string, '</script>');
  258. $this->assertEquals(3, $scripts);
  259. $scripts = substr_count($string, 'src="');
  260. $this->assertEquals(1, $scripts);
  261. $scripts = substr_count($string, '><');
  262. $this->assertEquals(1, $scripts);
  263. $this->assertContains('src="foo"', $string);
  264. $this->assertContains('bar', $string);
  265. $this->assertContains('baz', $string);
  266. $doc = new DOMDocument;
  267. $dom = $doc->loadHtml($string);
  268. $this->assertTrue($dom !== false);
  269. }
  270. public function testCapturingCapturesToObject()
  271. {
  272. $this->helper->captureStart();
  273. echo 'foobar';
  274. $this->helper->captureEnd();
  275. $values = $this->helper->getArrayCopy();
  276. $this->assertEquals(1, count($values), var_export($values, 1));
  277. $item = array_shift($values);
  278. $this->assertContains('foobar', $item->source);
  279. }
  280. public function testIndentationIsHonored()
  281. {
  282. $this->helper->setIndent(4);
  283. $this->helper->appendScript('
  284. var foo = "bar";
  285. document.write(foo.strlen());');
  286. $this->helper->appendScript('
  287. var bar = "baz";
  288. document.write(bar.strlen());');
  289. $string = $this->helper->toString();
  290. $scripts = substr_count($string, ' <script');
  291. $this->assertEquals(2, $scripts);
  292. $this->assertContains(' //', $string);
  293. $this->assertContains('var', $string);
  294. $this->assertContains('document', $string);
  295. $this->assertContains(' document', $string);
  296. }
  297. public function testDoesNotAllowDuplicateFiles()
  298. {
  299. $this->helper->headScript('FILE', '/js/prototype.js');
  300. $this->helper->headScript('FILE', '/js/prototype.js');
  301. $this->assertEquals(1, count($this->helper));
  302. }
  303. public function testRenderingDoesNotRenderArbitraryAttributesByDefault()
  304. {
  305. $this->helper->headScript()->appendFile('/js/foo.js', 'text/javascript', array('bogus' => 'deferred'));
  306. $test = $this->helper->headScript()->toString();
  307. $this->assertNotContains('bogus="deferred"', $test);
  308. }
  309. public function testCanRenderArbitraryAttributesOnRequest()
  310. {
  311. $this->helper->headScript()->appendFile('/js/foo.js', 'text/javascript', array('bogus' => 'deferred'))
  312. ->setAllowArbitraryAttributes(true);
  313. $test = $this->helper->headScript()->toString();
  314. $this->assertContains('bogus="deferred"', $test);
  315. }
  316. public function testCanPerformMultipleSerialCaptures()
  317. {
  318. $this->helper->headScript()->captureStart();
  319. echo "this is something captured";
  320. $this->helper->headScript()->captureEnd();
  321. try {
  322. $this->helper->headScript()->captureStart();
  323. } catch (Zend_View_Exception $e) {
  324. $this->fail('Serial captures should be allowed');
  325. }
  326. echo "this is something else captured";
  327. $this->helper->headScript()->captureEnd();
  328. }
  329. public function testCannotNestCaptures()
  330. {
  331. $this->helper->headScript()->captureStart();
  332. echo "this is something captured";
  333. try {
  334. $this->helper->headScript()->captureStart();
  335. $this->helper->headScript()->captureEnd();
  336. $this->fail('Should not be able to nest captures');
  337. } catch (Zend_View_Exception $e) {
  338. $this->helper->headScript()->captureEnd();
  339. $this->assertContains('Cannot nest', $e->getMessage());
  340. }
  341. $this->helper->headScript()->captureEnd();
  342. }
  343. /**
  344. * @issue ZF-3928
  345. * @link http://framework.zend.com/issues/browse/ZF-3928
  346. */
  347. public function testTurnOffAutoEscapeDoesNotEncodeAmpersand()
  348. {
  349. $this->helper->setAutoEscape(false)->appendFile('test.js?id=123&foo=bar');
  350. $this->assertEquals('<script type="text/javascript" src="test.js?id=123&foo=bar"></script>', $this->helper->toString());
  351. }
  352. public function testConditionalScript()
  353. {
  354. $this->helper->headScript()->appendFile('/js/foo.js', 'text/javascript', array('conditional' => 'lt IE 7'));
  355. $test = $this->helper->headScript()->toString();
  356. $this->assertContains('<!--[if lt IE 7]>', $test);
  357. }
  358. /**
  359. * @issue ZF-5435
  360. */
  361. public function testContainerMaintainsCorrectOrderOfItems()
  362. {
  363. $this->helper->offsetSetFile(1, 'test1.js');
  364. $this->helper->offsetSetFile(20, 'test2.js');
  365. $this->helper->offsetSetFile(10, 'test3.js');
  366. $this->helper->offsetSetFile(5, 'test4.js');
  367. $test = $this->helper->toString();
  368. $expected = '<script type="text/javascript" src="test1.js"></script>
  369. <script type="text/javascript" src="test4.js"></script>
  370. <script type="text/javascript" src="test3.js"></script>
  371. <script type="text/javascript" src="test2.js"></script>';
  372. $this->assertEquals($expected, $test);
  373. }
  374. }
  375. // Call Zend_View_Helper_HeadScriptTest::main() if this source file is executed directly.
  376. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadScriptTest::main") {
  377. Zend_View_Helper_HeadScriptTest::main();
  378. }