2
0

HeadLinkTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. // Call Zend_View_Helper_HeadLinkTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadLinkTest::main");
  5. }
  6. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. /** Zend_View_Helper_HeadLink */
  10. require_once 'Zend/View/Helper/HeadLink.php';
  11. /** Zend_View_Helper_Placeholder_Registry */
  12. require_once 'Zend/View/Helper/Placeholder/Registry.php';
  13. /** Zend_Registry */
  14. require_once 'Zend/Registry.php';
  15. /** Zend_View */
  16. require_once 'Zend/View.php';
  17. /**
  18. * Test class for Zend_View_Helper_HeadLink.
  19. *
  20. * @category Zend
  21. * @package Zend_View
  22. * @subpackage UnitTests
  23. */
  24. class Zend_View_Helper_HeadLinkTest extends PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * @var Zend_View_Helper_HeadLink
  28. */
  29. public $helper;
  30. /**
  31. * @var string
  32. */
  33. public $basePath;
  34. /**
  35. * Runs the test methods of this class.
  36. *
  37. * @return void
  38. */
  39. public static function main()
  40. {
  41. require_once "PHPUnit/TextUI/TestRunner.php";
  42. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadLinkTest");
  43. $result = PHPUnit_TextUI_TestRunner::run($suite);
  44. }
  45. /**
  46. * Sets up the fixture, for example, open a network connection.
  47. * This method is called before a test is executed.
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. foreach (array(Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY, 'Zend_View_Helper_Doctype') as $key) {
  54. if (Zend_Registry::isRegistered($key)) {
  55. $registry = Zend_Registry::getInstance();
  56. unset($registry[$key]);
  57. }
  58. }
  59. $this->basePath = dirname(__FILE__) . '/_files/modules';
  60. $this->view = new Zend_View();
  61. $this->helper = new Zend_View_Helper_HeadLink();
  62. $this->helper->setView($this->view);
  63. }
  64. /**
  65. * Tears down the fixture, for example, close a network connection.
  66. * This method is called after a test is executed.
  67. *
  68. * @return void
  69. */
  70. public function tearDown()
  71. {
  72. unset($this->helper);
  73. }
  74. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  75. {
  76. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  77. if ($registry->containerExists('Zend_View_Helper_HeadLink')) {
  78. $registry->deleteContainer('Zend_View_Helper_HeadLink');
  79. }
  80. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadLink'));
  81. $helper = new Zend_View_Helper_HeadLink();
  82. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadLink'));
  83. }
  84. public function testHeadLinkReturnsObjectInstance()
  85. {
  86. $placeholder = $this->helper->headLink();
  87. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadLink);
  88. }
  89. public function testPrependThrowsExceptionWithoutArrayArgument()
  90. {
  91. try {
  92. $this->helper->prepend('foo');
  93. $this->fail('prepend should raise exception without array argument');
  94. } catch (Exception $e) {
  95. }
  96. }
  97. public function testAppendThrowsExceptionWithoutArrayArgument()
  98. {
  99. try {
  100. $this->helper->append('foo');
  101. $this->fail('append should raise exception without array argument');
  102. } catch (Exception $e) {
  103. }
  104. }
  105. public function testSetThrowsExceptionWithoutArrayArgument()
  106. {
  107. try {
  108. $this->helper->set('foo');
  109. $this->fail('set should raise exception without array argument');
  110. } catch (Exception $e) {
  111. }
  112. }
  113. public function testOffsetSetThrowsExceptionWithoutArrayArgument()
  114. {
  115. try {
  116. $this->helper->offsetSet(1, 'foo');
  117. $this->fail('set should raise exception without array argument');
  118. } catch (Exception $e) {
  119. }
  120. }
  121. public function testCreatingLinkStackViaHeadScriptCreatesAppropriateOutput()
  122. {
  123. $links = array(
  124. 'link1' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  125. 'link2' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  126. 'link3' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  127. );
  128. $this->helper->headLink($links['link1'])
  129. ->headLink($links['link2'], 'PREPEND')
  130. ->headLink($links['link3']);
  131. $string = $this->helper->toString();
  132. $lines = substr_count($string, PHP_EOL);
  133. $this->assertEquals(2, $lines);
  134. $lines = substr_count($string, '<link ');
  135. $this->assertEquals(3, $lines, $string);
  136. foreach ($links as $link) {
  137. $substr = ' href="' . $link['href'] . '"';
  138. $this->assertContains($substr, $string);
  139. $substr = ' rel="' . $link['rel'] . '"';
  140. $this->assertContains($substr, $string);
  141. $substr = ' type="' . $link['type'] . '"';
  142. $this->assertContains($substr, $string);
  143. }
  144. $order = array();
  145. foreach ($this->helper as $key => $value) {
  146. if (isset($value->href)) {
  147. $order[$key] = $value->href;
  148. }
  149. }
  150. $expected = array('bar', 'foo', 'baz');
  151. $this->assertSame($expected, $order);
  152. }
  153. public function testCreatingLinkStackViaStyleSheetMethodsCreatesAppropriateOutput()
  154. {
  155. $links = array(
  156. 'link1' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  157. 'link2' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  158. 'link3' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  159. );
  160. $this->helper->appendStylesheet($links['link1']['href'])
  161. ->prependStylesheet($links['link2']['href'])
  162. ->appendStylesheet($links['link3']['href']);
  163. $string = $this->helper->toString();
  164. $lines = substr_count($string, PHP_EOL);
  165. $this->assertEquals(2, $lines);
  166. $lines = substr_count($string, '<link ');
  167. $this->assertEquals(3, $lines, $string);
  168. foreach ($links as $link) {
  169. $substr = ' href="' . $link['href'] . '"';
  170. $this->assertContains($substr, $string);
  171. $substr = ' rel="' . $link['rel'] . '"';
  172. $this->assertContains($substr, $string);
  173. $substr = ' type="' . $link['type'] . '"';
  174. $this->assertContains($substr, $string);
  175. }
  176. $order = array();
  177. foreach ($this->helper as $key => $value) {
  178. if (isset($value->href)) {
  179. $order[$key] = $value->href;
  180. }
  181. }
  182. $expected = array('bar', 'foo', 'baz');
  183. $this->assertSame($expected, $order);
  184. }
  185. public function testCreatingLinkStackViaAlternateMethodsCreatesAppropriateOutput()
  186. {
  187. $links = array(
  188. 'link1' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  189. 'link2' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  190. 'link3' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  191. );
  192. $where = 'append';
  193. foreach ($links as $link) {
  194. $method = $where . 'Alternate';
  195. $this->helper->$method($link['href'], $link['type'], $link['title']);
  196. $where = ('append' == $where) ? 'prepend' : 'append';
  197. }
  198. $string = $this->helper->toString();
  199. $lines = substr_count($string, PHP_EOL);
  200. $this->assertEquals(2, $lines);
  201. $lines = substr_count($string, '<link ');
  202. $this->assertEquals(3, $lines, $string);
  203. $lines = substr_count($string, ' rel="alternate"');
  204. $this->assertEquals(3, $lines, $string);
  205. foreach ($links as $link) {
  206. $substr = ' href="' . $link['href'] . '"';
  207. $this->assertContains($substr, $string);
  208. $substr = ' title="' . $link['title'] . '"';
  209. $this->assertContains($substr, $string);
  210. $substr = ' type="' . $link['type'] . '"';
  211. $this->assertContains($substr, $string);
  212. }
  213. $order = array();
  214. foreach ($this->helper as $key => $value) {
  215. if (isset($value->href)) {
  216. $order[$key] = $value->href;
  217. }
  218. }
  219. $expected = array('bar', 'foo', 'baz');
  220. $this->assertSame($expected, $order);
  221. }
  222. public function testOverloadingThrowsExceptionWithNoArguments()
  223. {
  224. try {
  225. $this->helper->appendStylesheet();
  226. $this->fail('Helper should expect at least one argument');
  227. } catch (Zend_View_Exception $e) {}
  228. }
  229. public function testOverloadingShouldAllowSingleArrayArgument()
  230. {
  231. $this->helper->setStylesheet(array('href' => '/styles.css'));
  232. $link = $this->helper->getValue();
  233. $this->assertEquals('/styles.css', $link->href);
  234. }
  235. public function testOverloadingUsingSingleArrayArgumentWithInvalidValuesThrowsException()
  236. {
  237. try {
  238. $this->helper->setStylesheet(array('bogus' => 'unused'));
  239. $this->fail('Invalid attribute values should raise exception');
  240. } catch (Zend_View_Exception $e) { }
  241. }
  242. public function testOverloadingOffsetSetWorks()
  243. {
  244. $this->helper->offsetSetStylesheet(100, '/styles.css');
  245. $items = $this->helper->getArrayCopy();
  246. $this->assertTrue(isset($items[100]));
  247. $link = $items[100];
  248. $this->assertEquals('/styles.css', $link->href);
  249. }
  250. public function testOverloadingThrowsExceptionWithInvalidMethod()
  251. {
  252. try {
  253. $this->helper->bogusMethod();
  254. $this->fail('Invalid method should raise exception');
  255. } catch (Zend_View_Exception $e) { }
  256. }
  257. public function testStylesheetAttributesGetSet()
  258. {
  259. $this->helper->setStylesheet('/styles.css', 'projection', 'ie6');
  260. $item = $this->helper->getValue();
  261. $this->assertObjectHasAttribute('media', $item);
  262. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  263. $this->assertEquals('projection', $item->media);
  264. $this->assertEquals('ie6', $item->conditionalStylesheet);
  265. }
  266. public function testConditionalStylesheetNotCreatedByDefault()
  267. {
  268. $this->helper->setStylesheet('/styles.css');
  269. $item = $this->helper->getValue();
  270. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  271. $this->assertFalse($item->conditionalStylesheet);
  272. $string = $this->helper->toString();
  273. $this->assertContains('/styles.css', $string);
  274. $this->assertNotContains('<!--[if', $string);
  275. $this->assertNotContains(']>', $string);
  276. $this->assertNotContains('<![endif]-->', $string);
  277. }
  278. public function testConditionalStylesheetCreationOccursWhenRequested()
  279. {
  280. $this->helper->setStylesheet('/styles.css', 'screen', 'ie6');
  281. $item = $this->helper->getValue();
  282. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  283. $this->assertEquals('ie6', $item->conditionalStylesheet);
  284. $string = $this->helper->toString();
  285. $this->assertContains('/styles.css', $string);
  286. $this->assertContains('<!--[if ie6]>', $string);
  287. $this->assertContains('<![endif]-->', $string);
  288. }
  289. public function testSettingAlternateWithTooFewArgsRaisesException()
  290. {
  291. try {
  292. $this->helper->setAlternate('foo');
  293. $this->fail('Setting alternate with fewer than 3 args should raise exception');
  294. } catch (Zend_View_Exception $e) { }
  295. try {
  296. $this->helper->setAlternate('foo', 'bar');
  297. $this->fail('Setting alternate with fewer than 3 args should raise exception');
  298. } catch (Zend_View_Exception $e) { }
  299. }
  300. public function testIndentationIsHonored()
  301. {
  302. $this->helper->setIndent(4);
  303. $this->helper->appendStylesheet('/css/screen.css');
  304. $this->helper->appendStylesheet('/css/rules.css');
  305. $string = $this->helper->toString();
  306. $scripts = substr_count($string, ' <link ');
  307. $this->assertEquals(2, $scripts);
  308. }
  309. public function testLinkRendersAsPlainHtmlIfDoctypeNotXhtml()
  310. {
  311. $this->view->doctype('HTML4_STRICT');
  312. $this->helper->headLink(array('rel' => 'icon', 'src' => '/foo/bar'))
  313. ->headLink(array('rel' => 'foo', 'href' => '/bar/baz'));
  314. $test = $this->helper->toString();
  315. $this->assertNotContains(' />', $test);
  316. }
  317. public function testDoesNotAllowDuplicateStylesheets()
  318. {
  319. $this->helper->appendStylesheet('foo');
  320. $this->helper->appendStylesheet('foo');
  321. $this->assertEquals(1, count($this->helper), var_export($this->helper->getContainer()->getArrayCopy(), 1));
  322. }
  323. /**
  324. * test for ZF-2889
  325. */
  326. public function testBooleanStylesheet()
  327. {
  328. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false));
  329. $test = $this->helper->toString();
  330. $this->assertNotContains('[if false]', $test);
  331. }
  332. /**
  333. * test for ZF-3271
  334. *
  335. */
  336. public function testBooleanTrueConditionalStylesheet()
  337. {
  338. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => true));
  339. $test = $this->helper->toString();
  340. $this->assertNotContains('[if 1]', $test);
  341. $this->assertNotContains('[if true]', $test);
  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)->appendStylesheet('/css/rules.css?id=123&foo=bar');
  350. $this->assertContains('id=123&foo=bar', $this->helper->toString());
  351. }
  352. public function testSetAlternateWithExtras()
  353. {
  354. $this->helper->setAlternate('/mydocument.pdf', 'application/pdf', 'foo', array('media' => array('print','screen')));
  355. $test = $this->helper->toString();
  356. $this->assertContains('media="print,screen"', $test);
  357. }
  358. public function testAppendStylesheetWithExtras()
  359. {
  360. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false, 'extras' => array('id' => 'my_link_tag')));
  361. $test = $this->helper->toString();
  362. $this->assertContains('id="my_link_tag"', $test);
  363. }
  364. public function testSetStylesheetWithMediaAsArray()
  365. {
  366. $this->helper->appendStylesheet('/bar/baz', array('screen','print'));
  367. $test = $this->helper->toString();
  368. $this->assertContains(' media="screen,print"', $test);
  369. }
  370. /**
  371. * @issue ZF-5435
  372. */
  373. public function testContainerMaintainsCorrectOrderOfItems()
  374. {
  375. $this->helper->headLink()->offsetSetStylesheet(1,'/test1.css');
  376. $this->helper->headLink()->offsetSetStylesheet(10,'/test2.css');
  377. $this->helper->headLink()->offsetSetStylesheet(20,'/test3.css');
  378. $this->helper->headLink()->offsetSetStylesheet(5,'/test4.css');
  379. $test = $this->helper->toString();
  380. $expected = '<link href="/test1.css" media="screen" rel="stylesheet" type="text/css" >
  381. <link href="/test4.css" media="screen" rel="stylesheet" type="text/css" >
  382. <link href="/test2.css" media="screen" rel="stylesheet" type="text/css" >
  383. <link href="/test3.css" media="screen" rel="stylesheet" type="text/css" >';
  384. $this->assertEquals($expected, $test);
  385. }
  386. }
  387. // Call Zend_View_Helper_HeadLinkTest::main() if this source file is executed directly.
  388. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadLinkTest::main") {
  389. Zend_View_Helper_HeadLinkTest::main();
  390. }