CommonHelperTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\View\View;
  4. use Tools\TestSuite\TestCase;
  5. use Tools\View\Helper\CommonHelper;
  6. /**
  7. * CommonHelper tests
  8. */
  9. class CommonHelperTest extends TestCase {
  10. /**
  11. * @var \Tools\View\Helper\CommonHelper
  12. */
  13. public $Common;
  14. /**
  15. * @return void
  16. */
  17. public function setUp() {
  18. parent::setUp();
  19. $View = new View(null);
  20. $this->Common = new CommonHelper($View);
  21. }
  22. /**
  23. * CommonHelperTest::testMetaRobots()
  24. *
  25. * @return void
  26. */
  27. public function testMetaRobots() {
  28. $result = $this->Common->metaRobots();
  29. $this->assertContains('<meta name="robots" content="', $result);
  30. }
  31. /**
  32. * CommonHelperTest::testMetaName()
  33. *
  34. * @return void
  35. */
  36. public function testMetaName() {
  37. $result = $this->Common->metaName('foo', [1, 2, 3]);
  38. $expected = '<meta name="foo" content="1, 2, 3" />';
  39. $this->assertEquals($expected, $result);
  40. }
  41. /**
  42. * CommonHelperTest::testMetaDescription()
  43. *
  44. * @return void
  45. */
  46. public function testMetaDescription() {
  47. $result = $this->Common->metaDescription('foo', 'deu');
  48. $expected = '<meta lang="deu" name="description" content="foo"/>';
  49. $this->assertEquals($expected, $result);
  50. }
  51. /**
  52. * CommonHelperTest::testMetaKeywords()
  53. *
  54. * @return void
  55. */
  56. public function testMetaKeywords() {
  57. $result = $this->Common->metaKeywords('foo bar', 'deu');
  58. $expected = '<meta lang="deu" name="keywords" content="foo bar"/>';
  59. $this->assertEquals($expected, $result);
  60. }
  61. /**
  62. * CommonHelperTest::testMetaRss()
  63. *
  64. * @return void
  65. */
  66. public function testMetaRss() {
  67. $result = $this->Common->metaRss('/some/url', 'some title');
  68. $expected = '<link rel="alternate" type="application/rss+xml" title="some title" href="/some/url" />';
  69. $this->assertEquals($expected, $result);
  70. }
  71. /**
  72. * CommonHelperTest::testMetaEquiv()
  73. *
  74. * @return void
  75. */
  76. public function testMetaEquiv() {
  77. $result = $this->Common->metaEquiv('type', 'value');
  78. $expected = '<meta http-equiv="type" content="value" />';
  79. $this->assertEquals($expected, $result);
  80. }
  81. /**
  82. * @return void
  83. */
  84. public function testMetaCanonical() {
  85. $is = $this->Common->metaCanonical('/some/url/param1');
  86. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url/param1') . '" rel="canonical"/>', trim($is));
  87. $is = $this->Common->metaCanonical('/some/url/param1', true);
  88. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url/param1', true) . '" rel="canonical"/>', trim($is));
  89. }
  90. /**
  91. * @return void
  92. */
  93. public function testMetaAlternate() {
  94. $is = $this->Common->metaAlternate('/some/url/param1', 'de-de', true);
  95. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url/param1', true) . '" rel="alternate" hreflang="de-de"/>', trim($is));
  96. $is = $this->Common->metaAlternate(['controller' => 'some', 'action' => 'url'], 'de', true);
  97. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url', true) . '" rel="alternate" hreflang="de"/>', trim($is));
  98. $is = $this->Common->metaAlternate(['controller' => 'some', 'action' => 'url'], ['de', 'de-ch'], true);
  99. $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));
  100. $is = $this->Common->metaAlternate(['controller' => 'some', 'action' => 'url'], ['de' => ['ch', 'at'], 'en' => ['gb', 'us']], true);
  101. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url', true) . '" rel="alternate" hreflang="de-ch"/>' . PHP_EOL .
  102. '<link href="' . $this->Common->Url->build('/some/url', true) . '" rel="alternate" hreflang="de-at"/>' . PHP_EOL .
  103. '<link href="' . $this->Common->Url->build('/some/url', true) . '" rel="alternate" hreflang="en-gb"/>' . PHP_EOL .
  104. '<link href="' . $this->Common->Url->build('/some/url', true) . '" rel="alternate" hreflang="en-us"/>', trim($is));
  105. }
  106. /**
  107. * CommonHelperTest::testAsp()
  108. *
  109. * @return void
  110. */
  111. public function testAsp() {
  112. $res = $this->Common->asp('House', 2, true);
  113. $expected = __d('tools', 'Houses');
  114. $this->assertEquals($expected, $res);
  115. $res = $this->Common->asp('House', 1, true);
  116. $expected = __d('tools', '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 = __d('tools', '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 = __d('tools', '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. }