CommonHelperTest.php 5.2 KB

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