HeadLinkTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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_HeadLinkTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadLinkTest::main");
  25. }
  26. /** Zend_View_Helper_HeadLink */
  27. require_once 'Zend/View/Helper/HeadLink.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. /** Zend_View */
  33. require_once 'Zend/View.php';
  34. /**
  35. * Test class for Zend_View_Helper_HeadLink.
  36. *
  37. * @category Zend
  38. * @package Zend_View
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_View
  43. * @group Zend_View_Helper
  44. */
  45. class Zend_View_Helper_HeadLinkTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * @var Zend_View_Helper_HeadLink
  49. */
  50. public $helper;
  51. /**
  52. * @var string
  53. */
  54. public $basePath;
  55. /**
  56. * Runs the test methods of this class.
  57. *
  58. * @return void
  59. */
  60. public static function main()
  61. {
  62. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadLinkTest");
  63. $result = PHPUnit_TextUI_TestRunner::run($suite);
  64. }
  65. /**
  66. * Sets up the fixture, for example, open a network connection.
  67. * This method is called before a test is executed.
  68. *
  69. * @return void
  70. */
  71. public function setUp()
  72. {
  73. foreach (array(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY, 'Zend_View_Helper_Doctype') as $key) {
  74. if (Zend_Registry::isRegistered($key)) {
  75. $registry = Zend_Registry::getInstance();
  76. unset($registry[$key]);
  77. }
  78. }
  79. $this->basePath = dirname(__FILE__) . '/_files/modules';
  80. $this->view = new Zend_View();
  81. $this->helper = new Zend_View_Helper_HeadLink();
  82. $this->helper->setView($this->view);
  83. }
  84. /**
  85. * Tears down the fixture, for example, close a network connection.
  86. * This method is called after a test is executed.
  87. *
  88. * @return void
  89. */
  90. public function tearDown()
  91. {
  92. unset($this->helper);
  93. }
  94. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  95. {
  96. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  97. if ($registry->containerExists('Zend_View_Helper_HeadLink')) {
  98. $registry->deleteContainer('Zend_View_Helper_HeadLink');
  99. }
  100. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadLink'));
  101. $helper = new Zend_View_Helper_HeadLink();
  102. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadLink'));
  103. }
  104. public function testHeadLinkReturnsObjectInstance()
  105. {
  106. $placeholder = $this->helper->headLink();
  107. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadLink);
  108. }
  109. public function testPrependThrowsExceptionWithoutArrayArgument()
  110. {
  111. try {
  112. $this->helper->prepend('foo');
  113. $this->fail('prepend should raise exception without array argument');
  114. } catch (Exception $e) {
  115. }
  116. }
  117. public function testAppendThrowsExceptionWithoutArrayArgument()
  118. {
  119. try {
  120. $this->helper->append('foo');
  121. $this->fail('append should raise exception without array argument');
  122. } catch (Exception $e) {
  123. }
  124. }
  125. public function testSetThrowsExceptionWithoutArrayArgument()
  126. {
  127. try {
  128. $this->helper->set('foo');
  129. $this->fail('set should raise exception without array argument');
  130. } catch (Exception $e) {
  131. }
  132. }
  133. public function testOffsetSetThrowsExceptionWithoutArrayArgument()
  134. {
  135. try {
  136. $this->helper->offsetSet(1, 'foo');
  137. $this->fail('set should raise exception without array argument');
  138. } catch (Exception $e) {
  139. }
  140. }
  141. public function testCreatingLinkStackViaHeadScriptCreatesAppropriateOutput()
  142. {
  143. $links = array(
  144. 'link1' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  145. 'link2' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  146. 'link3' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  147. );
  148. $this->helper->headLink($links['link1'])
  149. ->headLink($links['link2'], 'PREPEND')
  150. ->headLink($links['link3']);
  151. $string = $this->helper->toString();
  152. $lines = substr_count($string, PHP_EOL);
  153. $this->assertEquals(2, $lines);
  154. $lines = substr_count($string, '<link ');
  155. $this->assertEquals(3, $lines, $string);
  156. foreach ($links as $link) {
  157. $substr = ' href="' . $link['href'] . '"';
  158. $this->assertContains($substr, $string);
  159. $substr = ' rel="' . $link['rel'] . '"';
  160. $this->assertContains($substr, $string);
  161. $substr = ' type="' . $link['type'] . '"';
  162. $this->assertContains($substr, $string);
  163. }
  164. $order = array();
  165. foreach ($this->helper as $key => $value) {
  166. if (isset($value->href)) {
  167. $order[$key] = $value->href;
  168. }
  169. }
  170. $expected = array('bar', 'foo', 'baz');
  171. $this->assertSame($expected, $order);
  172. }
  173. public function testCreatingLinkStackViaStyleSheetMethodsCreatesAppropriateOutput()
  174. {
  175. $links = array(
  176. 'link1' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  177. 'link2' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  178. 'link3' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  179. );
  180. $this->helper->appendStylesheet($links['link1']['href'])
  181. ->prependStylesheet($links['link2']['href'])
  182. ->appendStylesheet($links['link3']['href']);
  183. $string = $this->helper->toString();
  184. $lines = substr_count($string, PHP_EOL);
  185. $this->assertEquals(2, $lines);
  186. $lines = substr_count($string, '<link ');
  187. $this->assertEquals(3, $lines, $string);
  188. foreach ($links as $link) {
  189. $substr = ' href="' . $link['href'] . '"';
  190. $this->assertContains($substr, $string);
  191. $substr = ' rel="' . $link['rel'] . '"';
  192. $this->assertContains($substr, $string);
  193. $substr = ' type="' . $link['type'] . '"';
  194. $this->assertContains($substr, $string);
  195. }
  196. $order = array();
  197. foreach ($this->helper as $key => $value) {
  198. if (isset($value->href)) {
  199. $order[$key] = $value->href;
  200. }
  201. }
  202. $expected = array('bar', 'foo', 'baz');
  203. $this->assertSame($expected, $order);
  204. }
  205. public function testCreatingLinkStackViaAlternateMethodsCreatesAppropriateOutput()
  206. {
  207. $links = array(
  208. 'link1' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  209. 'link2' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  210. 'link3' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  211. );
  212. $where = 'append';
  213. foreach ($links as $link) {
  214. $method = $where . 'Alternate';
  215. $this->helper->$method($link['href'], $link['type'], $link['title']);
  216. $where = ('append' == $where) ? 'prepend' : 'append';
  217. }
  218. $string = $this->helper->toString();
  219. $lines = substr_count($string, PHP_EOL);
  220. $this->assertEquals(2, $lines);
  221. $lines = substr_count($string, '<link ');
  222. $this->assertEquals(3, $lines, $string);
  223. $lines = substr_count($string, ' rel="alternate"');
  224. $this->assertEquals(3, $lines, $string);
  225. foreach ($links as $link) {
  226. $substr = ' href="' . $link['href'] . '"';
  227. $this->assertContains($substr, $string);
  228. $substr = ' title="' . $link['title'] . '"';
  229. $this->assertContains($substr, $string);
  230. $substr = ' type="' . $link['type'] . '"';
  231. $this->assertContains($substr, $string);
  232. }
  233. $order = array();
  234. foreach ($this->helper as $key => $value) {
  235. if (isset($value->href)) {
  236. $order[$key] = $value->href;
  237. }
  238. }
  239. $expected = array('bar', 'foo', 'baz');
  240. $this->assertSame($expected, $order);
  241. }
  242. public function testOverloadingThrowsExceptionWithNoArguments()
  243. {
  244. try {
  245. $this->helper->appendStylesheet();
  246. $this->fail('Helper should expect at least one argument');
  247. } catch (Zend_View_Exception $e) {}
  248. }
  249. public function testOverloadingShouldAllowSingleArrayArgument()
  250. {
  251. $this->helper->setStylesheet(array('href' => '/styles.css'));
  252. $link = $this->helper->getValue();
  253. $this->assertEquals('/styles.css', $link->href);
  254. }
  255. public function testOverloadingUsingSingleArrayArgumentWithInvalidValuesThrowsException()
  256. {
  257. try {
  258. $this->helper->setStylesheet(array('bogus' => 'unused'));
  259. $this->fail('Invalid attribute values should raise exception');
  260. } catch (Zend_View_Exception $e) { }
  261. }
  262. public function testOverloadingOffsetSetWorks()
  263. {
  264. $this->helper->offsetSetStylesheet(100, '/styles.css');
  265. $items = $this->helper->getArrayCopy();
  266. $this->assertTrue(isset($items[100]));
  267. $link = $items[100];
  268. $this->assertEquals('/styles.css', $link->href);
  269. }
  270. public function testOverloadingThrowsExceptionWithInvalidMethod()
  271. {
  272. try {
  273. $this->helper->bogusMethod();
  274. $this->fail('Invalid method should raise exception');
  275. } catch (Zend_View_Exception $e) { }
  276. }
  277. public function testStylesheetAttributesGetSet()
  278. {
  279. $this->helper->setStylesheet('/styles.css', 'projection', 'ie6');
  280. $item = $this->helper->getValue();
  281. $this->assertObjectHasAttribute('media', $item);
  282. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  283. $this->assertEquals('projection', $item->media);
  284. $this->assertEquals('ie6', $item->conditionalStylesheet);
  285. }
  286. public function testConditionalStylesheetNotCreatedByDefault()
  287. {
  288. $this->helper->setStylesheet('/styles.css');
  289. $item = $this->helper->getValue();
  290. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  291. $this->assertFalse($item->conditionalStylesheet);
  292. $string = $this->helper->toString();
  293. $this->assertContains('/styles.css', $string);
  294. $this->assertNotContains('<!--[if', $string);
  295. $this->assertNotContains(']>', $string);
  296. $this->assertNotContains('<![endif]-->', $string);
  297. }
  298. public function testConditionalStylesheetCreationOccursWhenRequested()
  299. {
  300. $this->helper->setStylesheet('/styles.css', 'screen', 'ie6');
  301. $item = $this->helper->getValue();
  302. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  303. $this->assertEquals('ie6', $item->conditionalStylesheet);
  304. $string = $this->helper->toString();
  305. $this->assertContains('/styles.css', $string);
  306. $this->assertContains('<!--[if ie6]>', $string);
  307. $this->assertContains('<![endif]-->', $string);
  308. }
  309. public function testSettingAlternateWithTooFewArgsRaisesException()
  310. {
  311. try {
  312. $this->helper->setAlternate('foo');
  313. $this->fail('Setting alternate with fewer than 3 args should raise exception');
  314. } catch (Zend_View_Exception $e) { }
  315. try {
  316. $this->helper->setAlternate('foo', 'bar');
  317. $this->fail('Setting alternate with fewer than 3 args should raise exception');
  318. } catch (Zend_View_Exception $e) { }
  319. }
  320. public function testIndentationIsHonored()
  321. {
  322. $this->helper->setIndent(4);
  323. $this->helper->appendStylesheet('/css/screen.css');
  324. $this->helper->appendStylesheet('/css/rules.css');
  325. $string = $this->helper->toString();
  326. $scripts = substr_count($string, ' <link ');
  327. $this->assertEquals(2, $scripts);
  328. }
  329. public function testLinkRendersAsPlainHtmlIfDoctypeNotXhtml()
  330. {
  331. $this->view->doctype('HTML4_STRICT');
  332. $this->helper->headLink(array('rel' => 'icon', 'src' => '/foo/bar'))
  333. ->headLink(array('rel' => 'foo', 'href' => '/bar/baz'));
  334. $test = $this->helper->toString();
  335. $this->assertNotContains(' />', $test);
  336. }
  337. public function testDoesNotAllowDuplicateStylesheets()
  338. {
  339. $this->helper->appendStylesheet('foo');
  340. $this->helper->appendStylesheet('foo');
  341. $this->assertEquals(1, count($this->helper), var_export($this->helper->getContainer()->getArrayCopy(), 1));
  342. }
  343. /**
  344. * test for ZF-2889
  345. */
  346. public function testBooleanStylesheet()
  347. {
  348. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false));
  349. $test = $this->helper->toString();
  350. $this->assertNotContains('[if false]', $test);
  351. }
  352. /**
  353. * test for ZF-3271
  354. *
  355. */
  356. public function testBooleanTrueConditionalStylesheet()
  357. {
  358. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => true));
  359. $test = $this->helper->toString();
  360. $this->assertNotContains('[if 1]', $test);
  361. $this->assertNotContains('[if true]', $test);
  362. }
  363. /**
  364. * @group ZF-3928
  365. * @link http://framework.zend.com/issues/browse/ZF-3928
  366. */
  367. public function testTurnOffAutoEscapeDoesNotEncodeAmpersand()
  368. {
  369. $this->helper->setAutoEscape(false)->appendStylesheet('/css/rules.css?id=123&foo=bar');
  370. $this->assertContains('id=123&foo=bar', $this->helper->toString());
  371. }
  372. public function testSetAlternateWithExtras()
  373. {
  374. $this->helper->setAlternate('/mydocument.pdf', 'application/pdf', 'foo', array('media' => array('print','screen')));
  375. $test = $this->helper->toString();
  376. $this->assertContains('media="print,screen"', $test);
  377. }
  378. public function testAppendStylesheetWithExtras()
  379. {
  380. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false, 'extras' => array('id' => 'my_link_tag')));
  381. $test = $this->helper->toString();
  382. $this->assertContains('id="my_link_tag"', $test);
  383. }
  384. public function testSetStylesheetWithMediaAsArray()
  385. {
  386. $this->helper->appendStylesheet('/bar/baz', array('screen','print'));
  387. $test = $this->helper->toString();
  388. $this->assertContains(' media="screen,print"', $test);
  389. }
  390. /**
  391. * @group ZF-5435
  392. */
  393. public function testContainerMaintainsCorrectOrderOfItems()
  394. {
  395. $this->helper->headLink()->offsetSetStylesheet(1,'/test1.css');
  396. $this->helper->headLink()->offsetSetStylesheet(10,'/test2.css');
  397. $this->helper->headLink()->offsetSetStylesheet(20,'/test3.css');
  398. $this->helper->headLink()->offsetSetStylesheet(5,'/test4.css');
  399. $test = $this->helper->toString();
  400. $expected = '<link href="/test1.css" media="screen" rel="stylesheet" type="text/css" >' . PHP_EOL
  401. . '<link href="/test4.css" media="screen" rel="stylesheet" type="text/css" >' . PHP_EOL
  402. . '<link href="/test2.css" media="screen" rel="stylesheet" type="text/css" >' . PHP_EOL
  403. . '<link href="/test3.css" media="screen" rel="stylesheet" type="text/css" >';
  404. $this->assertEquals($expected, $test);
  405. }
  406. /**
  407. * @group ZF-10345
  408. */
  409. public function testIdAttributeIsSupported()
  410. {
  411. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'id' => 'foo'));
  412. $this->assertContains('id="foo"', $this->helper->toString());
  413. }
  414. /**
  415. * @group ZF-11811
  416. */
  417. public function testHeadLinkAllowsOverrideOfRelAttribute()
  418. {
  419. $this->helper->appendStylesheet('/css/auth.less', 'all', null, array('rel' => 'stylesheet/less'));
  420. $this->assertEquals(1, substr_count($this->helper->toString(), "rel=\""));
  421. $this->assertContains('rel="stylesheet/less"', $this->helper->toString());
  422. }
  423. /**
  424. * @group ZF-11643
  425. */
  426. public function testSizesAttributeIsSupported()
  427. {
  428. $this->helper->headLink(
  429. array(
  430. 'rel' => 'icon',
  431. 'href' => 'favicon.png',
  432. 'sizes' => '16x16',
  433. 'type' => 'image/png',
  434. )
  435. );
  436. $expected = '<link href="favicon.png" rel="icon" type="image/png" sizes="16x16" >';
  437. $this->assertEquals($expected, $this->helper->toString());
  438. }
  439. /**
  440. * @group GH-515
  441. */
  442. public function testConditionalStylesheetCreationNoIE()
  443. {
  444. $this->helper->setStylesheet('/styles.css', 'screen', '!IE');
  445. $item = $this->helper->getValue();
  446. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  447. $this->assertEquals('!IE', $item->conditionalStylesheet);
  448. $string = $this->helper->toString();
  449. $this->assertContains('/styles.css', $string);
  450. $this->assertContains('<!--[if !IE]><!--><', $string);
  451. $this->assertContains('<!--<![endif]-->', $string);
  452. }
  453. /**
  454. * @group GH-515
  455. */
  456. public function testConditionalStylesheetCreationNoIEWidthSpaces()
  457. {
  458. $this->helper->setStylesheet('/styles.css', 'screen', '! IE');
  459. $item = $this->helper->getValue();
  460. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  461. $this->assertEquals('! IE', $item->conditionalStylesheet);
  462. $string = $this->helper->toString();
  463. $this->assertContains('/styles.css', $string);
  464. $this->assertContains('<!--[if ! IE]><!--><', $string);
  465. $this->assertContains('<!--<![endif]-->', $string);
  466. }
  467. }
  468. // Call Zend_View_Helper_HeadLinkTest::main() if this source file is executed directly.
  469. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadLinkTest::main") {
  470. Zend_View_Helper_HeadLinkTest::main();
  471. }