HeadLinkTest.php 17 KB

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