HeadStyleTest.php 15 KB

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