CommonHelperTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = ['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::testAlternate()
  21. *
  22. * @return void
  23. */
  24. public function testAlternate() {
  25. $result = $this->Common->alternate('one', 'two');
  26. $this->assertEquals('one', $result);
  27. $result = $this->Common->alternate('one', 'two');
  28. $this->assertEquals('two', $result);
  29. $result = $this->Common->alternate('one', 'two');
  30. $this->assertEquals('one', $result);
  31. }
  32. /**
  33. * CommonHelperTest::testMetaRobots()
  34. *
  35. * @return void
  36. */
  37. public function testMetaRobots() {
  38. $result = $this->Common->metaRobots();
  39. $this->assertContains('<meta name="robots" content="', $result);
  40. }
  41. /**
  42. * CommonHelperTest::testMetaName()
  43. *
  44. * @return void
  45. */
  46. public function testMetaName() {
  47. $result = $this->Common->metaName('foo', [1, 2, 3]);
  48. $expected = '<meta name="foo" content="1, 2, 3"/>';
  49. $this->assertEquals($expected, $result);
  50. }
  51. /**
  52. * CommonHelperTest::testMetaDescription()
  53. *
  54. * @return void
  55. */
  56. public function testMetaDescription() {
  57. $result = $this->Common->metaDescription('foo', 'deu');
  58. $expected = '<meta lang="deu" name="description" content="foo"/>';
  59. $this->assertEquals($expected, $result);
  60. }
  61. /**
  62. * CommonHelperTest::testMetaKeywords()
  63. *
  64. * @return void
  65. */
  66. public function testMetaKeywords() {
  67. $result = $this->Common->metaKeywords('foo bar', 'deu');
  68. $expected = '<meta lang="deu" name="keywords" content="foo bar"/>';
  69. $this->assertEquals($expected, $result);
  70. }
  71. /**
  72. * CommonHelperTest::testMetaRss()
  73. *
  74. * @return void
  75. */
  76. public function testMetaRss() {
  77. $result = $this->Common->metaRss('/some/url', 'some title');
  78. $expected = '<link rel="alternate" type="application/rss+xml" title="some title" href="/some/url"/>';
  79. $this->assertEquals($expected, $result);
  80. }
  81. /**
  82. * CommonHelperTest::testMetaEquiv()
  83. *
  84. * @return void
  85. */
  86. public function testMetaEquiv() {
  87. $result = $this->Common->metaEquiv('type', 'value');
  88. $expected = '<meta http-equiv="type" content="value"/>';
  89. $this->assertEquals($expected, $result);
  90. }
  91. /**
  92. * CommonHelperTest::testDisplayErrors()
  93. *
  94. * @return void
  95. */
  96. public function testDisplayErrors() {
  97. $result = $this->Common->displayErrors();
  98. $this->assertEquals('', $result);
  99. }
  100. /**
  101. * @return void
  102. */
  103. public function testMetaCanonical() {
  104. $is = $this->Common->metaCanonical('/some/url/param1');
  105. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1') . '" rel="canonical"/>', trim($is));
  106. $is = $this->Common->metaCanonical('/some/url/param1', true);
  107. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="canonical"/>', trim($is));
  108. }
  109. /**
  110. * @return void
  111. */
  112. public function testMetaAlternate() {
  113. $is = $this->Common->metaAlternate('/some/url/param1', 'de-de', true);
  114. $this->out(h($is));
  115. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="alternate" hreflang="de-de"/>', trim($is));
  116. $is = $this->Common->metaAlternate(['controller' => 'some', 'action' => 'url'], 'de', true);
  117. $this->out(h($is));
  118. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de"/>', trim($is));
  119. $is = $this->Common->metaAlternate(['controller' => 'some', 'action' => 'url'], ['de', 'de-ch'], true);
  120. $this->out(h($is));
  121. $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));
  122. $is = $this->Common->metaAlternate(['controller' => 'some', 'action' => 'url'], ['de' => ['ch', 'at'], 'en' => ['gb', 'us']], true);
  123. $this->out(h($is));
  124. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-ch"/>' . PHP_EOL .
  125. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-at"/>' . PHP_EOL .
  126. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-gb"/>' . PHP_EOL .
  127. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-us"/>', trim($is));
  128. }
  129. /**
  130. * @return void
  131. */
  132. public function testEsc() {
  133. $is = $this->Common->esc('Some Cool Text with <b>Html</b>');
  134. $this->assertEquals('Some Cool Text with &lt;b&gt;Html&lt;/b&gt;', $is);
  135. $is = $this->Common->esc('Some Cool Text' . PHP_EOL . 'with <b>Html</b>');
  136. $this->assertEquals('Some Cool Text<br />' . PHP_EOL . 'with &lt;b&gt;Html&lt;/b&gt;', $is);
  137. $is = $this->Common->esc('Some Cool' . PHP_EOL . ' 2 indends and' . PHP_EOL . ' 5 indends' . PHP_EOL . 'YEAH');
  138. $expected = 'Some Cool<br />' . PHP_EOL . '&nbsp;&nbsp;2 indends and<br />' . PHP_EOL . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5 indends<br />' . PHP_EOL . 'YEAH';
  139. $this->assertEquals($expected, $is);
  140. $options = ['tabsToSpaces' => 2];
  141. $is = $this->Common->esc('Some Cool' . PHP_EOL . "\t" . '1 tab and' . PHP_EOL . "\t" . "\t" . '2 tabs' . PHP_EOL . 'YEAH', $options);
  142. $expected = 'Some Cool<br />' . PHP_EOL . '&nbsp;&nbsp;1 tab and<br />' . PHP_EOL . '&nbsp;&nbsp;&nbsp;&nbsp;2 tabs<br />' . PHP_EOL . 'YEAH';
  143. $this->assertEquals($expected, $is);
  144. }
  145. /**
  146. * CommonHelperTest::testAsp()
  147. *
  148. * @return void
  149. */
  150. public function testAsp() {
  151. $res = $this->Common->asp('House', 2, true);
  152. $expected = __d('tools', 'Houses');
  153. $this->assertEquals($expected, $res);
  154. $res = $this->Common->asp('House', 1, true);
  155. $expected = __d('tools', 'House');
  156. $this->assertEquals($expected, $res);
  157. }
  158. /**
  159. * CommonHelperTest::testSp()
  160. *
  161. * @return void
  162. */
  163. public function testSp() {
  164. $res = $this->Common->sp('House', 'Houses', 0, true);
  165. $expected = __d('tools', 'Houses');
  166. $this->assertEquals($expected, $res);
  167. $res = $this->Common->sp('House', 'Houses', 2, true);
  168. $this->assertEquals($expected, $res);
  169. $res = $this->Common->sp('House', 'Houses', 1, true);
  170. $expected = __d('tools', 'House');
  171. $this->assertEquals($expected, $res);
  172. $res = $this->Common->sp('House', 'Houses', 1);
  173. $expected = 'House';
  174. $this->assertEquals($expected, $res);
  175. }
  176. /**
  177. * TearDown method
  178. *
  179. * @return void
  180. */
  181. public function tearDown() {
  182. parent::tearDown();
  183. unset($this->Common);
  184. }
  185. }