CommonHelperTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. App::uses('CommonHelper', 'Tools.View/Helper');
  3. App::uses('HtmlHelper', 'Tools.View/Helper');
  4. App::uses('View', 'View');
  5. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  6. /**
  7. * CommonHelper tests
  8. */
  9. class CommonHelperTest extends MyCakeTestCase {
  10. public $fixtures = array('core.cake_session');
  11. public $Common;
  12. public function setUp() {
  13. parent::setUp();
  14. Router::reload();
  15. $View = new View(null);
  16. $this->Common = new CommonHelper($View);
  17. $this->Html = new CommonHelper($View);
  18. }
  19. /**
  20. * CommonHelperTest::testFlashMessage()
  21. *
  22. * @return void
  23. */
  24. public function testFlashMessage() {
  25. $result = $this->Flash->message(h('Foo & bar'), 'success');
  26. $expected = '<div class="flash-messages flashMessages"><div class="message success">Foo &amp;amp; bar</div></div>';
  27. $this->assertEquals($expected, $result);
  28. }
  29. /**
  30. * CommonHelperTest::testAlternate()
  31. *
  32. * @return void
  33. */
  34. public function testAlternate() {
  35. $result = $this->Common->alternate('one', 'two');
  36. $this->assertEquals('one', $result);
  37. $result = $this->Common->alternate('one', 'two');
  38. $this->assertEquals('two', $result);
  39. $result = $this->Common->alternate('one', 'two');
  40. $this->assertEquals('one', $result);
  41. }
  42. /**
  43. * CommonHelperTest::testMetaRobots()
  44. *
  45. * @return void
  46. */
  47. public function testMetaRobots() {
  48. $result = $this->Common->metaRobots();
  49. $this->assertContains('<meta name="robots" content="', $result);
  50. }
  51. /**
  52. * CommonHelperTest::testMetaName()
  53. *
  54. * @return void
  55. */
  56. public function testMetaName() {
  57. $result = $this->Common->metaName('foo', array(1, 2, 3));
  58. $expected = '<meta name="foo" content="1, 2, 3" />';
  59. $this->assertEquals($expected, $result);
  60. }
  61. /**
  62. * CommonHelperTest::testMetaDescription()
  63. *
  64. * @return void
  65. */
  66. public function testMetaDescription() {
  67. $result = $this->Common->metaDescription('foo', 'deu');
  68. $expected = '<meta lang="deu" name="description" content="foo" />';
  69. $this->assertEquals($expected, $result);
  70. }
  71. /**
  72. * CommonHelperTest::testMetaKeywords()
  73. *
  74. * @return void
  75. */
  76. public function testMetaKeywords() {
  77. $result = $this->Common->metaKeywords('foo bar', 'deu');
  78. $expected = '<meta lang="deu" name="keywords" content="foo bar" />';
  79. $this->assertEquals($expected, $result);
  80. }
  81. /**
  82. * CommonHelperTest::testMetaRss()
  83. *
  84. * @return void
  85. */
  86. public function testMetaRss() {
  87. $result = $this->Common->metaRss('/some/url', 'some title');
  88. $expected = '<link rel="alternate" type="application/rss+xml" title="some title" href="/some/url" />';
  89. $this->assertEquals($expected, $result);
  90. }
  91. /**
  92. * CommonHelperTest::testMetaEquiv()
  93. *
  94. * @return void
  95. */
  96. public function testMetaEquiv() {
  97. $result = $this->Common->metaEquiv('type', 'value');
  98. $expected = '<meta http-equiv="type" content="value" />';
  99. $this->assertEquals($expected, $result);
  100. }
  101. /**
  102. * CommonHelperTest::testDisplayErrors()
  103. *
  104. * @return void
  105. */
  106. public function testDisplayErrors() {
  107. $result = $this->Common->displayErrors();
  108. $this->assertEquals('', $result);
  109. }
  110. /**
  111. * CommonHelperTest::testFlash()
  112. *
  113. * @return void
  114. */
  115. public function testFlash() {
  116. $this->Common->addFlashMessage(h('Foo & bar'), 'success');
  117. $result = $this->Common->flash();
  118. $expected = '<div class="flash-messages flashMessages"><div class="message success">Foo &amp; bar</div></div>';
  119. $this->assertEquals($expected, $result);
  120. $this->Common->addFlashMessage('I am an error', 'error');
  121. $this->Common->addFlashMessage('I am a warning', 'warning');
  122. $this->Common->addFlashMessage('I am some info', 'info');
  123. $this->Common->addFlashMessage('I am also some info');
  124. $this->Common->addFlashMessage('I am sth custom', 'custom');
  125. $result = $this->Common->flash();
  126. $this->assertTextContains('message error', $result);
  127. $this->assertTextContains('message warning', $result);
  128. $this->assertTextContains('message info', $result);
  129. $this->assertTextContains('message custom', $result);
  130. $result = substr_count($result, 'message info');
  131. $this->assertSame(2, $result);
  132. }
  133. /**
  134. * Test that you can define your own order or just output a subpart of
  135. * the types.
  136. *
  137. * @return void
  138. */
  139. public function testFlashWithTypes() {
  140. $this->Common->addFlashMessage('I am an error', 'error');
  141. $this->Common->addFlashMessage('I am a warning', 'warning');
  142. $this->Common->addFlashMessage('I am some info', 'info');
  143. $this->Common->addFlashMessage('I am also some info');
  144. $this->Common->addFlashMessage('I am sth custom', 'custom');
  145. $result = $this->Common->flash(array('warning', 'error'));
  146. $expected = '<div class="flash-messages flashMessages"><div class="message warning">I am a warning</div><div class="message error">I am an error</div></div>';
  147. $this->assertEquals($expected, $result);
  148. $result = $this->Common->flash(array('info'));
  149. $expected = '<div class="flash-messages flashMessages"><div class="message info">I am some info</div><div class="message info">I am also some info</div></div>';
  150. $this->assertEquals($expected, $result);
  151. $result = $this->Common->flash();
  152. $expected = '<div class="flash-messages flashMessages"><div class="message custom">I am sth custom</div></div>';
  153. $this->assertEquals($expected, $result);
  154. }
  155. /**
  156. * @return void
  157. */
  158. public function testMetaCanonical() {
  159. $is = $this->Common->metaCanonical('/some/url/param1');
  160. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1') . '" rel="canonical" />', trim($is));
  161. $is = $this->Common->metaCanonical('/some/url/param1', true);
  162. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="canonical" />', trim($is));
  163. }
  164. /**
  165. * @return void
  166. */
  167. public function testMetaAlternate() {
  168. $is = $this->Common->metaAlternate('/some/url/param1', 'de-de', true);
  169. $this->out(h($is));
  170. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="alternate" hreflang="de-de" />', trim($is));
  171. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), 'de', true);
  172. $this->out(h($is));
  173. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de" />', trim($is));
  174. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), array('de', 'de-ch'), true);
  175. $this->out(h($is));
  176. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de" />' . PHP_EOL . '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-ch" />', trim($is));
  177. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), array('de' => array('ch', 'at'), 'en' => array('gb', 'us')), true);
  178. $this->out(h($is));
  179. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-ch" />' . PHP_EOL .
  180. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-at" />' . PHP_EOL .
  181. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-gb" />' . PHP_EOL .
  182. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-us" />', trim($is));
  183. }
  184. /**
  185. * @return void
  186. */
  187. public function testEsc() {
  188. $is = $this->Common->esc('Some Cool Text with <b>Html</b>');
  189. $this->assertEquals($is, 'Some Cool Text with &lt;b&gt;Html&lt;/b&gt;');
  190. $is = $this->Common->esc('Some Cool Text' . PHP_EOL . 'with <b>Html</b>');
  191. $this->assertEquals($is, 'Some Cool Text<br />' . PHP_EOL . 'with &lt;b&gt;Html&lt;/b&gt;');
  192. $is = $this->Common->esc('Some Cool' . PHP_EOL . ' 2 indends and' . PHP_EOL . ' 5 indends' . PHP_EOL . 'YEAH');
  193. $this->assertEquals($is, 'Some Cool<br />' . PHP_EOL . '&nbsp;&nbsp;2 indends and<br />' . PHP_EOL . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 indends<br />' . PHP_EOL . 'YEAH');
  194. $options = array('tabsToSpaces' => 2);
  195. $is = $this->Common->esc('Some Cool' . PHP_EOL . TB . '1 tab and' . PHP_EOL . TB . TB . '2 tabs' . PHP_EOL . 'YEAH', $options);
  196. $this->assertEquals($is, 'Some Cool<br />' . PHP_EOL . '&nbsp;&nbsp;1 tab and<br />' . PHP_EOL . '&nbsp;&nbsp;&nbsp;&nbsp;2 tabs<br />' . PHP_EOL . 'YEAH');
  197. }
  198. /**
  199. * CommonHelperTest::testAsp()
  200. *
  201. * @return void
  202. */
  203. public function testAsp() {
  204. $res = $this->Common->asp('House', 2, true);
  205. $expected = __d('tools', 'Houses');
  206. $this->assertEquals($expected, $res);
  207. $res = $this->Common->asp('House', 1, true);
  208. $expected = __d('tools', 'House');
  209. $this->assertEquals($expected, $res);
  210. }
  211. /**
  212. * CommonHelperTest::testSp()
  213. *
  214. * @return void
  215. */
  216. public function testSp() {
  217. $res = $this->Common->sp('House', 'Houses', 0, true);
  218. $expected = __d('tools', 'Houses');
  219. $this->assertEquals($expected, $res);
  220. $res = $this->Common->sp('House', 'Houses', 2, true);
  221. $this->assertEquals($expected, $res);
  222. $res = $this->Common->sp('House', 'Houses', 1, true);
  223. $expected = __d('tools', 'House');
  224. $this->assertEquals($expected, $res);
  225. $res = $this->Common->sp('House', 'Houses', 1);
  226. $expected = 'House';
  227. $this->assertEquals($expected, $res);
  228. }
  229. /**
  230. * TearDown method
  231. *
  232. * @return void
  233. */
  234. public function tearDown() {
  235. parent::tearDown();
  236. unset($this->Common);
  237. }
  238. }