CommonHelperTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if (!Configure::read('App.fullBaseUrl')) {
  14. Configure::write('App.fullBaseUrl', 'http://localhost');
  15. }
  16. $View = new View(null);
  17. $this->Common = new CommonHelper($View);
  18. $this->Html = new CommonHelper($View);
  19. }
  20. /**
  21. * @return void
  22. */
  23. public function testMetaCanonical() {
  24. $this->skipIf(php_sapi_name() === 'cli', 'Strange result on travis - skip for now');
  25. $is = $this->Common->metaCanonical('/some/url/param1');
  26. $this->out(h($is));
  27. $this->assertEquals('<link href="' . Configure::read('App.fullBaseUrl') . $this->Html->url('/some/url/param1') . '" rel="canonical" />', trim($is));
  28. }
  29. /**
  30. * @return void
  31. */
  32. public function testMetaAlternate() {
  33. $this->skipIf(php_sapi_name() === 'cli', 'Strange result on travis - skip for now');
  34. $is = $this->Common->metaAlternate('/some/url/param1', 'de-de', true);
  35. $this->out(h($is));
  36. $this->assertEquals('<link href="' . $this->Html->url('/some/url/param1', true) . '" rel="alternate" hreflang="de-de" />', trim($is));
  37. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), 'de', true);
  38. $this->out(h($is));
  39. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de" />', trim($is));
  40. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), array('de', 'de-ch'), true);
  41. $this->out(h($is));
  42. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de" />' . PHP_EOL . '<link href="' . FULL_BASE_URL . $this->Html->url('/some/url') . '" rel="alternate" hreflang="de-ch" />', trim($is));
  43. $is = $this->Common->metaAlternate(array('controller' => 'some', 'action' => 'url'), array('de' => array('ch', 'at'), 'en' => array('gb', 'us')), true);
  44. $this->out(h($is));
  45. $this->assertEquals('<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-ch" />' . PHP_EOL .
  46. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="de-at" />' . PHP_EOL .
  47. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-gb" />' . PHP_EOL .
  48. '<link href="' . $this->Html->url('/some/url', true) . '" rel="alternate" hreflang="en-us" />', trim($is));
  49. }
  50. /**
  51. * @return void
  52. */
  53. public function testEsc() {
  54. $is = $this->Common->esc('Some Cool Text with <b>Html</b>');
  55. $this->assertEquals($is, 'Some Cool Text with &lt;b&gt;Html&lt;/b&gt;');
  56. $is = $this->Common->esc('Some Cool Text' . PHP_EOL . 'with <b>Html</b>');
  57. $this->assertEquals($is, 'Some Cool Text<br />' . PHP_EOL . 'with &lt;b&gt;Html&lt;/b&gt;');
  58. $is = $this->Common->esc('Some Cool' . PHP_EOL . ' 2 indends and' . PHP_EOL . ' 5 indends' . PHP_EOL . 'YEAH');
  59. $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');
  60. $options = array('tabsToSpaces' => 2);
  61. $is = $this->Common->esc('Some Cool' . PHP_EOL . TB . '1 tab and' . PHP_EOL . TB . TB . '2 tabs' . PHP_EOL . 'YEAH', $options);
  62. $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');
  63. }
  64. /**
  65. * CommonHelperTest::testAsp()
  66. *
  67. * @return void
  68. */
  69. public function testAsp() {
  70. $res = $this->Common->asp('House', 2, true);
  71. $expected = __('Houses');
  72. $this->assertEquals($expected, $res);
  73. $res = $this->Common->asp('House', 1, true);
  74. $expected = __('House');
  75. $this->assertEquals($expected, $res);
  76. }
  77. /**
  78. * CommonHelperTest::testSp()
  79. *
  80. * @return void
  81. */
  82. public function testSp() {
  83. $res = $this->Common->sp('House', 'Houses', 0, true);
  84. $expected = __('Houses');
  85. $this->assertEquals($expected, $res);
  86. $res = $this->Common->sp('House', 'Houses', 2, true);
  87. $this->assertEquals($expected, $res);
  88. $res = $this->Common->sp('House', 'Houses', 1, true);
  89. $expected = __('House');
  90. $this->assertEquals($expected, $res);
  91. $res = $this->Common->sp('House', 'Houses', 1);
  92. $expected = 'House';
  93. $this->assertEquals($expected, $res);
  94. }
  95. /**
  96. * TearDown method
  97. *
  98. * @return void
  99. */
  100. public function tearDown() {
  101. parent::tearDown();
  102. unset($this->Common);
  103. }
  104. }