CommonHelperTest.php 6.5 KB

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