CommonHelperTest.php 7.5 KB

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