CommonHelperTest.php 5.1 KB

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