CommonHelperTest.php 5.2 KB

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