PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/View/Helper/HeadLinkTest.php

https://github.com/praveenuniyal/zf2
PHP | 416 lines | 312 code | 52 blank | 52 comment | 4 complexity | 88df225228e3d1353d48522e7ae6e993 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_View
  9. */
  10. namespace ZendTest\View\Helper;
  11. use Zend\View\Helper\Placeholder\Registry as PlaceholderRegistry;
  12. use Zend\View\Helper;
  13. use Zend\View\Renderer\PhpRenderer as View;
  14. use Zend\View\Exception\ExceptionInterface as ViewException;
  15. /**
  16. * Test class for Zend_View_Helper_HeadLink.
  17. *
  18. * @category Zend
  19. * @package Zend_View
  20. * @subpackage UnitTests
  21. * @group Zend_View
  22. * @group Zend_View_Helper
  23. */
  24. class 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. * Sets up the fixture, for example, open a network connection.
  36. * This method is called before a test is executed.
  37. *
  38. * @return void
  39. */
  40. public function setUp()
  41. {
  42. Helper\Doctype::unsetDoctypeRegistry();
  43. $this->basePath = __DIR__ . '/_files/modules';
  44. $this->view = new View();
  45. $this->helper = new Helper\HeadLink();
  46. $this->helper->setView($this->view);
  47. }
  48. /**
  49. * Tears down the fixture, for example, close a network connection.
  50. * This method is called after a test is executed.
  51. *
  52. * @return void
  53. */
  54. public function tearDown()
  55. {
  56. unset($this->helper);
  57. }
  58. public function testHeadLinkReturnsObjectInstance()
  59. {
  60. $placeholder = $this->helper->__invoke();
  61. $this->assertTrue($placeholder instanceof Helper\HeadLink);
  62. }
  63. public function testPrependThrowsExceptionWithoutArrayArgument()
  64. {
  65. $this->setExpectedException('Zend\View\Exception\ExceptionInterface');
  66. $this->helper->prepend('foo');
  67. }
  68. public function testAppendThrowsExceptionWithoutArrayArgument()
  69. {
  70. $this->setExpectedException('Zend\View\Exception\ExceptionInterface');
  71. $this->helper->append('foo');
  72. }
  73. public function testSetThrowsExceptionWithoutArrayArgument()
  74. {
  75. $this->setExpectedException('Zend\View\Exception\ExceptionInterface');
  76. $this->helper->set('foo');
  77. }
  78. public function testOffsetSetThrowsExceptionWithoutArrayArgument()
  79. {
  80. $this->setExpectedException('Zend\View\Exception\ExceptionInterface');
  81. $this->helper->offsetSet(1, 'foo');
  82. }
  83. public function testCreatingLinkStackViaHeadLinkCreatesAppropriateOutput()
  84. {
  85. $links = array(
  86. 'link1' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  87. 'link2' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  88. 'link3' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  89. );
  90. $this->helper->headLink($links['link1'])
  91. ->headLink($links['link2'], 'PREPEND')
  92. ->headLink($links['link3']);
  93. $string = $this->helper->toString();
  94. $lines = substr_count($string, PHP_EOL);
  95. $this->assertEquals(2, $lines);
  96. $lines = substr_count($string, '<link ');
  97. $this->assertEquals(3, $lines, $string);
  98. foreach ($links as $link) {
  99. $substr = ' href="' . $link['href'] . '"';
  100. $this->assertContains($substr, $string);
  101. $substr = ' rel="' . $link['rel'] . '"';
  102. $this->assertContains($substr, $string);
  103. $substr = ' type="' . $link['type'] . '"';
  104. $this->assertContains($substr, $string);
  105. }
  106. $order = array();
  107. foreach ($this->helper as $key => $value) {
  108. if (isset($value->href)) {
  109. $order[$key] = $value->href;
  110. }
  111. }
  112. $expected = array('bar', 'foo', 'baz');
  113. $this->assertSame($expected, $order);
  114. }
  115. public function testCreatingLinkStackViaStyleSheetMethodsCreatesAppropriateOutput()
  116. {
  117. $links = array(
  118. 'link1' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  119. 'link2' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  120. 'link3' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  121. );
  122. $this->helper->appendStylesheet($links['link1']['href'])
  123. ->prependStylesheet($links['link2']['href'])
  124. ->appendStylesheet($links['link3']['href']);
  125. $string = $this->helper->toString();
  126. $lines = substr_count($string, PHP_EOL);
  127. $this->assertEquals(2, $lines);
  128. $lines = substr_count($string, '<link ');
  129. $this->assertEquals(3, $lines, $string);
  130. foreach ($links as $link) {
  131. $substr = ' href="' . $link['href'] . '"';
  132. $this->assertContains($substr, $string);
  133. $substr = ' rel="' . $link['rel'] . '"';
  134. $this->assertContains($substr, $string);
  135. $substr = ' type="' . $link['type'] . '"';
  136. $this->assertContains($substr, $string);
  137. }
  138. $order = array();
  139. foreach ($this->helper as $key => $value) {
  140. if (isset($value->href)) {
  141. $order[$key] = $value->href;
  142. }
  143. }
  144. $expected = array('bar', 'foo', 'baz');
  145. $this->assertSame($expected, $order);
  146. }
  147. public function testCreatingLinkStackViaAlternateMethodsCreatesAppropriateOutput()
  148. {
  149. $links = array(
  150. 'link1' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'foo'),
  151. 'link2' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'bar'),
  152. 'link3' => array('title' => 'stylesheet', 'type' => 'text/css', 'href' => 'baz'),
  153. );
  154. $where = 'append';
  155. foreach ($links as $link) {
  156. $method = $where . 'Alternate';
  157. $this->helper->$method($link['href'], $link['type'], $link['title']);
  158. $where = ('append' == $where) ? 'prepend' : 'append';
  159. }
  160. $string = $this->helper->toString();
  161. $lines = substr_count($string, PHP_EOL);
  162. $this->assertEquals(2, $lines);
  163. $lines = substr_count($string, '<link ');
  164. $this->assertEquals(3, $lines, $string);
  165. $lines = substr_count($string, ' rel="alternate"');
  166. $this->assertEquals(3, $lines, $string);
  167. foreach ($links as $link) {
  168. $substr = ' href="' . $link['href'] . '"';
  169. $this->assertContains($substr, $string);
  170. $substr = ' title="' . $link['title'] . '"';
  171. $this->assertContains($substr, $string);
  172. $substr = ' type="' . $link['type'] . '"';
  173. $this->assertContains($substr, $string);
  174. }
  175. $order = array();
  176. foreach ($this->helper as $key => $value) {
  177. if (isset($value->href)) {
  178. $order[$key] = $value->href;
  179. }
  180. }
  181. $expected = array('bar', 'foo', 'baz');
  182. $this->assertSame($expected, $order);
  183. }
  184. public function testOverloadingThrowsExceptionWithNoArguments()
  185. {
  186. $this->setExpectedException('Zend\View\Exception\ExceptionInterface');
  187. $this->helper->appendStylesheet();
  188. }
  189. public function testOverloadingShouldAllowSingleArrayArgument()
  190. {
  191. $this->helper->setStylesheet(array('href' => '/styles.css'));
  192. $link = $this->helper->getValue();
  193. $this->assertEquals('/styles.css', $link->href);
  194. }
  195. public function testOverloadingUsingSingleArrayArgumentWithInvalidValuesThrowsException()
  196. {
  197. $this->setExpectedException('Zend\View\Exception\ExceptionInterface');
  198. $this->helper->setStylesheet(array('bogus' => 'unused'));
  199. }
  200. public function testOverloadingOffsetSetWorks()
  201. {
  202. $this->helper->offsetSetStylesheet(100, '/styles.css');
  203. $items = $this->helper->getArrayCopy();
  204. $this->assertTrue(isset($items[100]));
  205. $link = $items[100];
  206. $this->assertEquals('/styles.css', $link->href);
  207. }
  208. public function testOverloadingThrowsExceptionWithInvalidMethod()
  209. {
  210. $this->setExpectedException('Zend\View\Exception\ExceptionInterface');
  211. $this->helper->bogusMethod();
  212. }
  213. public function testStylesheetAttributesGetSet()
  214. {
  215. $this->helper->setStylesheet('/styles.css', 'projection', 'ie6');
  216. $item = $this->helper->getValue();
  217. $this->assertObjectHasAttribute('media', $item);
  218. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  219. $this->assertEquals('projection', $item->media);
  220. $this->assertEquals('ie6', $item->conditionalStylesheet);
  221. }
  222. public function testConditionalStylesheetNotCreatedByDefault()
  223. {
  224. $this->helper->setStylesheet('/styles.css');
  225. $item = $this->helper->getValue();
  226. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  227. $this->assertFalse($item->conditionalStylesheet);
  228. $string = $this->helper->toString();
  229. $this->assertContains('/styles.css', $string);
  230. $this->assertNotContains('<!--[if', $string);
  231. $this->assertNotContains(']>', $string);
  232. $this->assertNotContains('<![endif]-->', $string);
  233. }
  234. public function testConditionalStylesheetCreationOccursWhenRequested()
  235. {
  236. $this->helper->setStylesheet('/styles.css', 'screen', 'ie6');
  237. $item = $this->helper->getValue();
  238. $this->assertObjectHasAttribute('conditionalStylesheet', $item);
  239. $this->assertEquals('ie6', $item->conditionalStylesheet);
  240. $string = $this->helper->toString();
  241. $this->assertContains('/styles.css', $string);
  242. $this->assertContains('<!--[if ie6]>', $string);
  243. $this->assertContains('<![endif]-->', $string);
  244. }
  245. public function testSettingAlternateWithTooFewArgsRaisesException()
  246. {
  247. try {
  248. $this->helper->setAlternate('foo');
  249. $this->fail('Setting alternate with fewer than 3 args should raise exception');
  250. } catch (ViewException $e) { }
  251. try {
  252. $this->helper->setAlternate('foo', 'bar');
  253. $this->fail('Setting alternate with fewer than 3 args should raise exception');
  254. } catch (ViewException $e) { }
  255. }
  256. public function testIndentationIsHonored()
  257. {
  258. $this->helper->setIndent(4);
  259. $this->helper->appendStylesheet('/css/screen.css');
  260. $this->helper->appendStylesheet('/css/rules.css');
  261. $string = $this->helper->toString();
  262. $scripts = substr_count($string, ' <link ');
  263. $this->assertEquals(2, $scripts);
  264. }
  265. public function testLinkRendersAsPlainHtmlIfDoctypeNotXhtml()
  266. {
  267. $this->view->plugin('doctype')->__invoke('HTML4_STRICT');
  268. $this->helper->__invoke(array('rel' => 'icon', 'src' => '/foo/bar'))
  269. ->__invoke(array('rel' => 'foo', 'href' => '/bar/baz'));
  270. $test = $this->helper->toString();
  271. $this->assertNotContains(' />', $test);
  272. }
  273. public function testDoesNotAllowDuplicateStylesheets()
  274. {
  275. $this->helper->appendStylesheet('foo');
  276. $this->helper->appendStylesheet('foo');
  277. $this->assertEquals(1, count($this->helper), var_export($this->helper->getContainer()->getArrayCopy(), 1));
  278. }
  279. /**
  280. * test for ZF-2889
  281. */
  282. public function testBooleanStylesheet()
  283. {
  284. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false));
  285. $test = $this->helper->toString();
  286. $this->assertNotContains('[if false]', $test);
  287. }
  288. /**
  289. * test for ZF-3271
  290. *
  291. */
  292. public function testBooleanTrueConditionalStylesheet()
  293. {
  294. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => true));
  295. $test = $this->helper->toString();
  296. $this->assertNotContains('[if 1]', $test);
  297. $this->assertNotContains('[if true]', $test);
  298. }
  299. /**
  300. * @issue ZF-3928
  301. * @link http://framework.zend.com/issues/browse/ZF-3928
  302. */
  303. public function testTurnOffAutoEscapeDoesNotEncodeAmpersand()
  304. {
  305. $this->helper->setAutoEscape(false)->appendStylesheet('/css/rules.css?id=123&foo=bar');
  306. $this->assertContains('id=123&foo=bar', $this->helper->toString());
  307. }
  308. public function testSetAlternateWithExtras()
  309. {
  310. $this->helper->setAlternate('/mydocument.pdf', 'application/pdf', 'foo', array('media' => array('print','screen')));
  311. $test = $this->helper->toString();
  312. $this->assertContains('media="print,screen"', $test);
  313. }
  314. public function testAppendStylesheetWithExtras()
  315. {
  316. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'conditionalStylesheet' => false, 'extras' => array('id' => 'my_link_tag')));
  317. $test = $this->helper->toString();
  318. $this->assertContains('id="my_link_tag"', $test);
  319. }
  320. public function testSetStylesheetWithMediaAsArray()
  321. {
  322. $this->helper->appendStylesheet('/bar/baz', array('screen','print'));
  323. $test = $this->helper->toString();
  324. $this->assertContains(' media="screen,print"', $test);
  325. }
  326. public function testSetPrevRelationship()
  327. {
  328. $this->helper->appendPrev('/foo/bar');
  329. $test = $this->helper->toString();
  330. $this->assertContains('href="/foo/bar"', $test);
  331. $this->assertContains('rel="prev"', $test);
  332. }
  333. public function testSetNextRelationship()
  334. {
  335. $this->helper->appendNext('/foo/bar');
  336. $test = $this->helper->toString();
  337. $this->assertContains('href="/foo/bar"', $test);
  338. $this->assertContains('rel="next"', $test);
  339. }
  340. /**
  341. * @issue ZF-5435
  342. */
  343. public function testContainerMaintainsCorrectOrderOfItems()
  344. {
  345. $this->helper->__invoke()->offsetSetStylesheet(1,'/test1.css');
  346. $this->helper->__invoke()->offsetSetStylesheet(10,'/test2.css');
  347. $this->helper->__invoke()->offsetSetStylesheet(20,'/test3.css');
  348. $this->helper->__invoke()->offsetSetStylesheet(5,'/test4.css');
  349. $test = $this->helper->toString();
  350. $expected = '<link href="/test1.css" media="screen" rel="stylesheet" type="text/css">' . PHP_EOL
  351. . '<link href="/test4.css" media="screen" rel="stylesheet" type="text/css">' . PHP_EOL
  352. . '<link href="/test2.css" media="screen" rel="stylesheet" type="text/css">' . PHP_EOL
  353. . '<link href="/test3.css" media="screen" rel="stylesheet" type="text/css">';
  354. $this->assertEquals($expected, $test);
  355. }
  356. /**
  357. * @issue ZF-10345
  358. */
  359. public function testIdAttributeIsSupported()
  360. {
  361. $this->helper->appendStylesheet(array('href' => '/bar/baz', 'id' => 'foo'));
  362. $this->assertContains('id="foo"', $this->helper->toString());
  363. }
  364. }