CommonHelperTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. }
  56. /**
  57. * @return void
  58. */
  59. public function testMetaCanonical() {
  60. $is = $this->Common->metaCanonical('/some/url/param1');
  61. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1') . '" rel="canonical" />', trim($is));
  62. $is = $this->Common->metaCanonical('/some/url/param1', true);
  63. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="canonical" />', trim($is));
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function testMetaAlternate() {
  69. $is = $this->Common->metaAlternate('/some/url/param1', 'de-de', true);
  70. $this->out(h($is));
  71. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="alternate" hreflang="de-de" />', trim($is));
  72. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), 'de', true);
  73. $this->out(h($is));
  74. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de" />', trim($is));
  75. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), array('de', 'de-ch'), true);
  76. $this->out(h($is));
  77. $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));
  78. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), array('de' => array('ch', 'at'), 'en' => array('gb', 'us')), true);
  79. $this->out(h($is));
  80. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-ch" />' . PHP_EOL .
  81. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-at" />' . PHP_EOL .
  82. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-gb" />' . PHP_EOL .
  83. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-us" />', trim($is));
  84. }
  85. /**
  86. * @return void
  87. */
  88. public function testEsc() {
  89. $is = $this->Common->esc('Some Cool Text with <b>Html</b>');
  90. $this->assertEquals($is, 'Some Cool Text with &lt;b&gt;Html&lt;/b&gt;');
  91. $is = $this->Common->esc('Some Cool Text' . PHP_EOL . 'with <b>Html</b>');
  92. $this->assertEquals($is, 'Some Cool Text<br />' . PHP_EOL . 'with &lt;b&gt;Html&lt;/b&gt;');
  93. $is = $this->Common->esc('Some Cool' . PHP_EOL . ' 2 indends and' . PHP_EOL . ' 5 indends' . PHP_EOL . 'YEAH');
  94. $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');
  95. $options = array('tabsToSpaces' => 2);
  96. $is = $this->Common->esc('Some Cool' . PHP_EOL . TB . '1 tab and' . PHP_EOL . TB . TB . '2 tabs' . PHP_EOL . 'YEAH', $options);
  97. $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');
  98. }
  99. /**
  100. * CommonHelperTest::testAsp()
  101. *
  102. * @return void
  103. */
  104. public function testAsp() {
  105. $res = $this->Common->asp('House', 2, true);
  106. $expected = __('Houses');
  107. $this->assertEquals($expected, $res);
  108. $res = $this->Common->asp('House', 1, true);
  109. $expected = __('House');
  110. $this->assertEquals($expected, $res);
  111. }
  112. /**
  113. * CommonHelperTest::testSp()
  114. *
  115. * @return void
  116. */
  117. public function testSp() {
  118. $res = $this->Common->sp('House', 'Houses', 0, true);
  119. $expected = __('Houses');
  120. $this->assertEquals($expected, $res);
  121. $res = $this->Common->sp('House', 'Houses', 2, true);
  122. $this->assertEquals($expected, $res);
  123. $res = $this->Common->sp('House', 'Houses', 1, true);
  124. $expected = __('House');
  125. $this->assertEquals($expected, $res);
  126. $res = $this->Common->sp('House', 'Houses', 1);
  127. $expected = 'House';
  128. $this->assertEquals($expected, $res);
  129. }
  130. /**
  131. * TearDown method
  132. *
  133. * @return void
  134. */
  135. public function tearDown() {
  136. parent::tearDown();
  137. unset($this->Common);
  138. }
  139. }