CommonHelperTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 $Common;
  11. public function setUp() {
  12. parent::setUp();
  13. $View = new View(null);
  14. $this->Common = new CommonHelper($View);
  15. $this->Html = new CommonHelper($View);
  16. }
  17. /**
  18. * CommonHelperTest::testFlash()
  19. *
  20. * @return void
  21. */
  22. public function testFlash() {
  23. $this->Common->addFlashMessage(h('Foo & bar'), 'success');
  24. $result = $this->Common->flash();
  25. $expected = '<div class="flashMessages"><div class="message success">Foo &amp; bar</div></div>';
  26. $this->assertEquals($expected, $result);
  27. $this->Common->addFlashMessage('I am an error', 'error');
  28. $this->Common->addFlashMessage('I am a warning', 'warning');
  29. $this->Common->addFlashMessage('I am some info', 'info');
  30. $this->Common->addFlashMessage('I am also some info');
  31. $this->Common->addFlashMessage('I am sth custom', 'custom');
  32. $result = $this->Common->flash();
  33. $this->assertTextContains('message error', $result);
  34. $this->assertTextContains('message warning', $result);
  35. $this->assertTextContains('message info', $result);
  36. $this->assertTextContains('message custom', $result);
  37. $result = substr_count($result, 'message info');
  38. $this->assertSame(2, $result);
  39. }
  40. /**
  41. * Test that you can define your own order or just output a subpart of
  42. * the types.
  43. *
  44. * @return void
  45. */
  46. public function testFlashWithTypes() {
  47. $this->Common->addFlashMessage('I am an error', 'error');
  48. $this->Common->addFlashMessage('I am a warning', 'warning');
  49. $this->Common->addFlashMessage('I am some info', 'info');
  50. $this->Common->addFlashMessage('I am also some info');
  51. $this->Common->addFlashMessage('I am sth custom', 'custom');
  52. $result = $this->Common->flash(array('warning', 'error'));
  53. $expected = '<div class="flashMessages"><div class="message warning">I am a warning</div><div class="message error">I am an error</div></div>';
  54. $this->assertEquals($expected, $result);
  55. $result = $this->Common->flash(array('info'));
  56. $expected = '<div class="flashMessages"><div class="message info">I am some info</div><div class="message info">I am also some info</div></div>';
  57. $this->assertEquals($expected, $result);
  58. $result = $this->Common->flash();
  59. $expected = '<div class="flashMessages"><div class="message custom">I am sth custom</div></div>';
  60. $this->assertEquals($expected, $result);
  61. }
  62. /**
  63. * @return void
  64. */
  65. public function testMetaCanonical() {
  66. $is = $this->Common->metaCanonical('/some/url/param1');
  67. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1') . '" rel="canonical" />', trim($is));
  68. $is = $this->Common->metaCanonical('/some/url/param1', true);
  69. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="canonical" />', trim($is));
  70. }
  71. /**
  72. * @return void
  73. */
  74. public function testMetaAlternate() {
  75. $is = $this->Common->metaAlternate('/some/url/param1', 'de-de', true);
  76. $this->out(h($is));
  77. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="alternate" hreflang="de-de" />', trim($is));
  78. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), 'de', true);
  79. $this->out(h($is));
  80. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de" />', trim($is));
  81. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), array('de', 'de-ch'), true);
  82. $this->out(h($is));
  83. $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));
  84. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), array('de' => array('ch', 'at'), 'en' => array('gb', 'us')), true);
  85. $this->out(h($is));
  86. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-ch" />' . PHP_EOL .
  87. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-at" />' . PHP_EOL .
  88. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-gb" />' . PHP_EOL .
  89. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-us" />', trim($is));
  90. }
  91. /**
  92. * @return void
  93. */
  94. public function testEsc() {
  95. $is = $this->Common->esc('Some Cool Text with <b>Html</b>');
  96. $this->assertEquals($is, 'Some Cool Text with &lt;b&gt;Html&lt;/b&gt;');
  97. $is = $this->Common->esc('Some Cool Text' . PHP_EOL . 'with <b>Html</b>');
  98. $this->assertEquals($is, 'Some Cool Text<br />' . PHP_EOL . 'with &lt;b&gt;Html&lt;/b&gt;');
  99. $is = $this->Common->esc('Some Cool' . PHP_EOL . ' 2 indends and' . PHP_EOL . ' 5 indends' . PHP_EOL . 'YEAH');
  100. $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');
  101. $options = array('tabsToSpaces' => 2);
  102. $is = $this->Common->esc('Some Cool' . PHP_EOL . TB . '1 tab and' . PHP_EOL . TB . TB . '2 tabs' . PHP_EOL . 'YEAH', $options);
  103. $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');
  104. }
  105. /**
  106. * CommonHelperTest::testAsp()
  107. *
  108. * @return void
  109. */
  110. public function testAsp() {
  111. $res = $this->Common->asp('House', 2, true);
  112. $expected = __('Houses');
  113. $this->assertEquals($expected, $res);
  114. $res = $this->Common->asp('House', 1, true);
  115. $expected = __('House');
  116. $this->assertEquals($expected, $res);
  117. }
  118. /**
  119. * CommonHelperTest::testSp()
  120. *
  121. * @return void
  122. */
  123. public function testSp() {
  124. $res = $this->Common->sp('House', 'Houses', 0, true);
  125. $expected = __('Houses');
  126. $this->assertEquals($expected, $res);
  127. $res = $this->Common->sp('House', 'Houses', 2, true);
  128. $this->assertEquals($expected, $res);
  129. $res = $this->Common->sp('House', 'Houses', 1, true);
  130. $expected = __('House');
  131. $this->assertEquals($expected, $res);
  132. $res = $this->Common->sp('House', 'Houses', 1);
  133. $expected = 'House';
  134. $this->assertEquals($expected, $res);
  135. }
  136. /**
  137. * TearDown method
  138. *
  139. * @return void
  140. */
  141. public function tearDown() {
  142. parent::tearDown();
  143. unset($this->Common);
  144. }
  145. }