HeadStyleTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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_HeadStyleTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_HeadStyleTest::main");
  25. }
  26. /** Zend_View_Helper_HeadStyle */
  27. require_once 'Zend/View/Helper/HeadStyle.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. /**
  33. * Test class for Zend_View_Helper_HeadStyle.
  34. *
  35. * @category Zend
  36. * @package Zend_View
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_View
  41. * @group Zend_View_Helper
  42. */
  43. class Zend_View_Helper_HeadStyleTest extends PHPUnit_Framework_TestCase
  44. {
  45. /**
  46. * @var Zend_View_Helper_HeadStyle
  47. */
  48. public $helper;
  49. /**
  50. * @var string
  51. */
  52. public $basePath;
  53. /**
  54. * Runs the test methods of this class.
  55. *
  56. * @return void
  57. */
  58. public static function main()
  59. {
  60. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_HeadStyleTest");
  61. $result = PHPUnit_TextUI_TestRunner::run($suite);
  62. }
  63. /**
  64. * Sets up the fixture, for example, open a network connection.
  65. * This method is called before a test is executed.
  66. *
  67. * @return void
  68. */
  69. public function setUp()
  70. {
  71. $regKey = Zend_View_Helper_Placeholder_Registry::REGISTRY_KEY;
  72. if (Zend_Registry::isRegistered($regKey)) {
  73. $registry = Zend_Registry::getInstance();
  74. unset($registry[$regKey]);
  75. }
  76. $this->basePath = dirname(__FILE__) . '/_files/modules';
  77. $this->helper = new Zend_View_Helper_HeadStyle();
  78. }
  79. /**
  80. * Tears down the fixture, for example, close a network connection.
  81. * This method is called after a test is executed.
  82. *
  83. * @return void
  84. */
  85. public function tearDown()
  86. {
  87. unset($this->helper);
  88. }
  89. public function testNamespaceRegisteredInPlaceholderRegistryAfterInstantiation()
  90. {
  91. $registry = Zend_View_Helper_Placeholder_Registry::getRegistry();
  92. if ($registry->containerExists('Zend_View_Helper_HeadStyle')) {
  93. $registry->deleteContainer('Zend_View_Helper_HeadStyle');
  94. }
  95. $this->assertFalse($registry->containerExists('Zend_View_Helper_HeadStyle'));
  96. $helper = new Zend_View_Helper_HeadStyle();
  97. $this->assertTrue($registry->containerExists('Zend_View_Helper_HeadStyle'));
  98. }
  99. public function testHeadStyleReturnsObjectInstance()
  100. {
  101. $placeholder = $this->helper->headStyle();
  102. $this->assertTrue($placeholder instanceof Zend_View_Helper_HeadStyle);
  103. }
  104. public function testAppendPrependAndSetThrowExceptionsWhenNonStyleValueProvided()
  105. {
  106. try {
  107. $this->helper->append('foo');
  108. $this->fail('Non-style value should not append');
  109. } catch (Zend_View_Exception $e) { }
  110. try {
  111. $this->helper->offsetSet(5, 'foo');
  112. $this->fail('Non-style value should not offsetSet');
  113. } catch (Zend_View_Exception $e) { }
  114. try {
  115. $this->helper->prepend('foo');
  116. $this->fail('Non-style value should not prepend');
  117. } catch (Zend_View_Exception $e) { }
  118. try {
  119. $this->helper->set('foo');
  120. $this->fail('Non-style value should not set');
  121. } catch (Zend_View_Exception $e) { }
  122. }
  123. public function testOverloadAppendStyleAppendsStyleToStack()
  124. {
  125. $string = 'a {}';
  126. for ($i = 0; $i < 3; ++$i) {
  127. $string .= PHP_EOL . 'a {}';
  128. $this->helper->appendStyle($string);
  129. $values = $this->helper->getArrayCopy();
  130. $this->assertEquals($i + 1, count($values));
  131. $item = $values[$i];
  132. $this->assertTrue($item instanceof stdClass);
  133. $this->assertObjectHasAttribute('content', $item);
  134. $this->assertObjectHasAttribute('attributes', $item);
  135. $this->assertEquals($string, $item->content);
  136. }
  137. }
  138. public function testOverloadPrependStylePrependsStyleToStack()
  139. {
  140. $string = 'a {}';
  141. for ($i = 0; $i < 3; ++$i) {
  142. $string .= PHP_EOL . 'a {}';
  143. $this->helper->prependStyle($string);
  144. $values = $this->helper->getArrayCopy();
  145. $this->assertEquals($i + 1, count($values));
  146. $item = array_shift($values);
  147. $this->assertTrue($item instanceof stdClass);
  148. $this->assertObjectHasAttribute('content', $item);
  149. $this->assertObjectHasAttribute('attributes', $item);
  150. $this->assertEquals($string, $item->content);
  151. }
  152. }
  153. public function testOverloadSetOversitesStack()
  154. {
  155. $string = 'a {}';
  156. for ($i = 0; $i < 3; ++$i) {
  157. $this->helper->appendStyle($string);
  158. $string .= PHP_EOL . 'a {}';
  159. }
  160. $this->helper->setStyle($string);
  161. $values = $this->helper->getArrayCopy();
  162. $this->assertEquals(1, count($values));
  163. $item = array_shift($values);
  164. $this->assertTrue($item instanceof stdClass);
  165. $this->assertObjectHasAttribute('content', $item);
  166. $this->assertObjectHasAttribute('attributes', $item);
  167. $this->assertEquals($string, $item->content);
  168. }
  169. public function testCanBuildStyleTagsWithAttributes()
  170. {
  171. $this->helper->setStyle('a {}', array(
  172. 'lang' => 'us_en',
  173. 'title' => 'foo',
  174. 'media' => 'projection',
  175. 'dir' => 'rtol',
  176. 'bogus' => 'unused'
  177. ));
  178. $value = $this->helper->getValue();
  179. $this->assertObjectHasAttribute('attributes', $value);
  180. $attributes = $value->attributes;
  181. $this->assertTrue(isset($attributes['lang']));
  182. $this->assertTrue(isset($attributes['title']));
  183. $this->assertTrue(isset($attributes['media']));
  184. $this->assertTrue(isset($attributes['dir']));
  185. $this->assertTrue(isset($attributes['bogus']));
  186. $this->assertEquals('us_en', $attributes['lang']);
  187. $this->assertEquals('foo', $attributes['title']);
  188. $this->assertEquals('projection', $attributes['media']);
  189. $this->assertEquals('rtol', $attributes['dir']);
  190. $this->assertEquals('unused', $attributes['bogus']);
  191. }
  192. public function testRenderedStyleTagsContainHtmlEscaping()
  193. {
  194. $this->helper->setStyle('a {}', array(
  195. 'lang' => 'us_en',
  196. 'title' => 'foo',
  197. 'media' => 'screen',
  198. 'dir' => 'rtol',
  199. 'bogus' => 'unused'
  200. ));
  201. $value = $this->helper->toString();
  202. $this->assertContains('<!--' . PHP_EOL, $value);
  203. $this->assertContains(PHP_EOL . '-->', $value);
  204. }
  205. public function testRenderedStyleTagsContainsDefaultMedia()
  206. {
  207. $this->helper->setStyle('a {}', array(
  208. ));
  209. $value = $this->helper->toString();
  210. $this->assertRegexp('#<style [^>]*?media="screen"#', $value, $value);
  211. }
  212. /**
  213. * @group ZF-8056
  214. */
  215. public function testMediaAttributeCanHaveSpaceInCommaSeparatedString()
  216. {
  217. $this->helper->appendStyle('a { }', array('media' => 'screen, projection'));
  218. $string = $this->helper->toString();
  219. $this->assertContains('media="screen,projection"', $string);
  220. }
  221. public function testHeadStyleProxiesProperly()
  222. {
  223. $style1 = 'a {}';
  224. $style2 = 'a {}' . PHP_EOL . 'h1 {}';
  225. $style3 = 'a {}' . PHP_EOL . 'h2 {}';
  226. $this->helper->headStyle($style1, 'SET')
  227. ->headStyle($style2, 'PREPEND')
  228. ->headStyle($style3, 'APPEND');
  229. $this->assertEquals(3, count($this->helper));
  230. $values = $this->helper->getArrayCopy();
  231. $this->assertTrue((strstr($values[0]->content, $style2)) ? true : false);
  232. $this->assertTrue((strstr($values[1]->content, $style1)) ? true : false);
  233. $this->assertTrue((strstr($values[2]->content, $style3)) ? true : false);
  234. }
  235. public function testToStyleGeneratesValidHtml()
  236. {
  237. $style1 = 'a {}';
  238. $style2 = 'body {}' . PHP_EOL . 'h1 {}';
  239. $style3 = 'div {}' . PHP_EOL . 'li {}';
  240. $this->helper->headStyle($style1, 'SET')
  241. ->headStyle($style2, 'PREPEND')
  242. ->headStyle($style3, 'APPEND');
  243. $html = $this->helper->toString();
  244. $doc = new DOMDocument;
  245. $dom = $doc->loadHtml($html);
  246. $this->assertTrue(($dom !== false));
  247. $styles = substr_count($html, '<style type="text/css"');
  248. $this->assertEquals(3, $styles);
  249. $styles = substr_count($html, '</style>');
  250. $this->assertEquals(3, $styles);
  251. $this->assertContains($style3, $html);
  252. $this->assertContains($style2, $html);
  253. $this->assertContains($style1, $html);
  254. }
  255. public function testCapturingCapturesToObject()
  256. {
  257. $this->helper->captureStart();
  258. echo 'foobar';
  259. $this->helper->captureEnd();
  260. $values = $this->helper->getArrayCopy();
  261. $this->assertEquals(1, count($values));
  262. $item = array_shift($values);
  263. $this->assertContains('foobar', $item->content);
  264. }
  265. public function testOverloadingOffsetSetWritesToSpecifiedIndex()
  266. {
  267. $this->helper->offsetSetStyle(100, 'foobar');
  268. $values = $this->helper->getArrayCopy();
  269. $this->assertEquals(1, count($values));
  270. $this->assertTrue(isset($values[100]));
  271. $item = $values[100];
  272. $this->assertContains('foobar', $item->content);
  273. }
  274. public function testInvalidMethodRaisesException()
  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 testTooFewArgumentsRaisesException()
  282. {
  283. try {
  284. $this->helper->appendStyle();
  285. $this->fail('Too few arguments should raise exception');
  286. } catch (Zend_View_Exception $e) { }
  287. }
  288. public function testIndentationIsHonored()
  289. {
  290. $this->helper->setIndent(4);
  291. $this->helper->appendStyle('
  292. a {
  293. display: none;
  294. }');
  295. $this->helper->appendStyle('
  296. h1 {
  297. font-weight: bold
  298. }');
  299. $string = $this->helper->toString();
  300. $scripts = substr_count($string, ' <style');
  301. $this->assertEquals(2, $scripts);
  302. $this->assertContains(' <!--', $string);
  303. $this->assertContains(' a {', $string);
  304. $this->assertContains(' h1 {', $string);
  305. $this->assertContains(' display', $string);
  306. $this->assertContains(' font-weight', $string);
  307. $this->assertContains(' }', $string);
  308. }
  309. public function testSerialCapturingWorks()
  310. {
  311. $this->helper->headStyle()->captureStart();
  312. echo "Captured text";
  313. $this->helper->headStyle()->captureEnd();
  314. try {
  315. $this->helper->headStyle()->captureStart();
  316. } catch (Zend_View_Exception $e) {
  317. $this->fail('Serial capturing should work');
  318. }
  319. $this->helper->headStyle()->captureEnd();
  320. }
  321. public function testNestedCapturingFails()
  322. {
  323. $this->helper->headStyle()->captureStart();
  324. echo "Captured text";
  325. try {
  326. $this->helper->headStyle()->captureStart();
  327. $this->helper->headStyle()->captureEnd();
  328. $this->fail('Nested capturing should fail');
  329. } catch (Zend_View_Exception $e) {
  330. $this->helper->headStyle()->captureEnd();
  331. $this->assertContains('Cannot nest', $e->getMessage());
  332. }
  333. }
  334. public function testMediaAttributeAsArray()
  335. {
  336. $this->helper->setIndent(4);
  337. $this->helper->appendStyle('
  338. a {
  339. display: none;
  340. }', array('media' => array('screen', 'projection')));
  341. $string = $this->helper->toString();
  342. $scripts = substr_count($string, ' <style');
  343. $this->assertEquals(1, $scripts);
  344. $this->assertContains(' <!--', $string);
  345. $this->assertContains(' a {', $string);
  346. $this->assertContains(' media="screen,projection"', $string);
  347. }
  348. public function testMediaAttributeAsCommaSeperatedString()
  349. {
  350. $this->helper->setIndent(4);
  351. $this->helper->appendStyle('
  352. a {
  353. display: none;
  354. }', array('media' => 'screen,projection'));
  355. $string = $this->helper->toString();
  356. $scripts = substr_count($string, ' <style');
  357. $this->assertEquals(1, $scripts);
  358. $this->assertContains(' <!--', $string);
  359. $this->assertContains(' a {', $string);
  360. $this->assertContains(' media="screen,projection"', $string);
  361. }
  362. public function testConditionalScript()
  363. {
  364. $this->helper->appendStyle('
  365. a {
  366. display: none;
  367. }', array('media' => 'screen,projection', 'conditional' => 'lt IE 7'));
  368. $test = $this->helper->toString();
  369. $this->assertContains('<!--[if lt IE 7]>', $test);
  370. }
  371. /**
  372. * @group ZF-5435
  373. */
  374. public function testContainerMaintainsCorrectOrderOfItems()
  375. {
  376. $style1 = 'a {display: none;}';
  377. $this->helper->offsetSetStyle(10, $style1);
  378. $style2 = 'h1 {font-weight: bold}';
  379. $this->helper->offsetSetStyle(5, $style2);
  380. $test = $this->helper->toString();
  381. $expected = '<style type="text/css" media="screen">' . PHP_EOL
  382. . '<!--' . PHP_EOL
  383. . $style2 . PHP_EOL
  384. . '-->' . PHP_EOL
  385. . '</style>' . PHP_EOL
  386. . '<style type="text/css" media="screen">' . PHP_EOL
  387. . '<!--' . PHP_EOL
  388. . $style1 . PHP_EOL
  389. . '-->' . PHP_EOL
  390. . '</style>';
  391. $this->assertEquals($expected, $test);
  392. }
  393. /**
  394. * @group ZF-9532
  395. */
  396. public function testRenderConditionalCommentsShouldNotContainHtmlEscaping()
  397. {
  398. $style = 'a{display:none;}';
  399. $this->helper->appendStyle($style, array(
  400. 'conditional' => 'IE 8'
  401. ));
  402. $value = $this->helper->toString();
  403. $this->assertNotContains('<!--' . PHP_EOL, $value);
  404. $this->assertNotContains(PHP_EOL . '-->', $value);
  405. }
  406. /**
  407. * @group GH-515
  408. */
  409. public function testConditionalScriptNoIE()
  410. {
  411. $this->helper->appendStyle('
  412. a {
  413. display: none;
  414. }', array('media' => 'screen,projection', 'conditional' => '!IE'));
  415. $test = $this->helper->toString();
  416. $this->assertContains('<!--[if !IE]><!--><', $test);
  417. $this->assertContains('<!--<![endif]-->', $test);
  418. }
  419. /**
  420. * @group GH-515
  421. */
  422. public function testConditionalScriptNoIEWidthSpace()
  423. {
  424. $this->helper->appendStyle('
  425. a {
  426. display: none;
  427. }', array('media' => 'screen,projection', 'conditional' => '! IE'));
  428. $test = $this->helper->toString();
  429. $this->assertContains('<!--[if ! IE]><!--><', $test);
  430. $this->assertContains('<!--<![endif]-->', $test);
  431. }
  432. }
  433. // Call Zend_View_Helper_HeadStyleTest::main() if this source file is executed directly.
  434. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_HeadStyleTest::main") {
  435. Zend_View_Helper_HeadStyleTest::main();
  436. }