CommonHelperTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Shim\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. $builder = Router::createRouteBuilder('/');
  25. $builder->scope('/', function(RouteBuilder $routes) {
  26. $routes->fallbacks(DashedRoute::class);
  27. });
  28. }
  29. /**
  30. * CommonHelperTest::testMetaRobots()
  31. *
  32. * @return void
  33. */
  34. public function testMetaRobots() {
  35. $result = $this->Common->metaRobots();
  36. $this->assertStringContainsString('<meta name="robots" content="', $result);
  37. }
  38. /**
  39. * CommonHelperTest::testMetaName()
  40. *
  41. * @return void
  42. */
  43. public function testMetaName() {
  44. $result = $this->Common->metaName('foo', [1, 2, 3]);
  45. $expected = '<meta name="foo" content="1, 2, 3">';
  46. $this->assertEquals($expected, $result);
  47. }
  48. /**
  49. * CommonHelperTest::testMetaDescription()
  50. *
  51. * @return void
  52. */
  53. public function testMetaDescription() {
  54. $result = $this->Common->metaDescription('foo', 'deu');
  55. $expected = '<meta lang="deu" name="description" content="foo">';
  56. $this->assertEquals($expected, $result);
  57. }
  58. /**
  59. * CommonHelperTest::testMetaKeywords()
  60. *
  61. * @return void
  62. */
  63. public function testMetaKeywords() {
  64. $result = $this->Common->metaKeywords('foo bar', 'deu');
  65. $expected = '<meta lang="deu" name="keywords" content="foo bar">';
  66. $this->assertEquals($expected, $result);
  67. }
  68. /**
  69. * CommonHelperTest::testMetaRss()
  70. *
  71. * @return void
  72. */
  73. public function testMetaRss() {
  74. $result = $this->Common->metaRss('/some/url', 'some title');
  75. $expected = '<link rel="alternate" type="application/rss+xml" title="some title" href="/some/url">';
  76. $this->assertEquals($expected, $result);
  77. }
  78. /**
  79. * CommonHelperTest::testMetaEquiv()
  80. *
  81. * @return void
  82. */
  83. public function testMetaEquiv() {
  84. $result = $this->Common->metaEquiv('type', 'value');
  85. $expected = '<meta http-equiv="type" content="value">';
  86. $this->assertEquals($expected, $result);
  87. }
  88. /**
  89. * @return void
  90. */
  91. public function testMetaCanonical() {
  92. $is = $this->Common->metaCanonical('/some/url/param1');
  93. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url/param1') . '" rel="canonical">', trim($is));
  94. $is = $this->Common->metaCanonical('/some/url/param1', true);
  95. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url/param1', ['fullBase' => true]) . '" rel="canonical">', trim($is));
  96. }
  97. /**
  98. * @return void
  99. */
  100. public function testMetaAlternate() {
  101. $is = $this->Common->metaAlternate('/some/url/param1', 'de-de', true);
  102. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url/param1', ['fullBase' => true]) . '" rel="alternate" hreflang="de-de">', trim($is));
  103. $is = $this->Common->metaAlternate(['controller' => 'Some', 'action' => 'url'], 'de', true);
  104. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url', ['fullBase' => true]) . '" rel="alternate" hreflang="de">', trim($is));
  105. $is = $this->Common->metaAlternate(['controller' => 'Some', 'action' => 'url'], ['de', 'de-ch'], true);
  106. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url', ['fullBase' => true]) . '" rel="alternate" hreflang="de">' . PHP_EOL . '<link href="' . $this->Common->Url->build('/some/url', ['fullBase' => true]) . '" rel="alternate" hreflang="de-ch">', trim($is));
  107. $is = $this->Common->metaAlternate(['controller' => 'Some', 'action' => 'url'], ['de' => ['ch', 'at'], 'en' => ['gb', 'us']], true);
  108. $this->assertEquals('<link href="' . $this->Common->Url->build('/some/url', ['fullBase' => true]) . '" rel="alternate" hreflang="de-ch">' . PHP_EOL .
  109. '<link href="' . $this->Common->Url->build('/some/url', ['fullBase' => true]) . '" rel="alternate" hreflang="de-at">' . PHP_EOL .
  110. '<link href="' . $this->Common->Url->build('/some/url', ['fullBase' => true]) . '" rel="alternate" hreflang="en-gb">' . PHP_EOL .
  111. '<link href="' . $this->Common->Url->build('/some/url', ['fullBase' => true]) . '" rel="alternate" hreflang="en-us">', trim($is));
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function testSp() {
  117. $res = $this->Common->sp('House', 'Houses', 0, true);
  118. $expected = __d('tools', 'Houses');
  119. $this->assertEquals($expected, $res);
  120. $res = $this->Common->sp('House', 'Houses', 2, true);
  121. $this->assertEquals($expected, $res);
  122. $res = $this->Common->sp('House', 'Houses', 1, true);
  123. $expected = __d('tools', 'House');
  124. $this->assertEquals($expected, $res);
  125. $res = $this->Common->sp('House', 'Houses', 1);
  126. $expected = 'House';
  127. $this->assertEquals($expected, $res);
  128. }
  129. /**
  130. * TearDown method
  131. *
  132. * @return void
  133. */
  134. public function tearDown(): void {
  135. parent::tearDown();
  136. unset($this->Common);
  137. }
  138. }