SimplesitemapTestBase.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Drupal\Tests\simple_sitemap\Functional;
  3. use Drupal\Core\StringTranslation\StringTranslationTrait;
  4. use Drupal\Tests\BrowserTestBase;
  5. use Drupal\language\Entity\ConfigurableLanguage;
  6. /**
  7. * Provides the base class for web tests for Simple sitemap.
  8. */
  9. abstract class SimplesitemapTestBase extends BrowserTestBase {
  10. use StringTranslationTrait;
  11. /**
  12. * Modules to enable for this test.
  13. *
  14. * @var string[]
  15. */
  16. public static $modules = [
  17. 'simple_sitemap',
  18. 'node',
  19. 'content_translation',
  20. ];
  21. /**
  22. * Simple sitemap generator.
  23. *
  24. * @var \Drupal\simple_sitemap\Simplesitemap
  25. */
  26. protected $generator;
  27. /**
  28. * Database service.
  29. *
  30. * @var \Drupal\Core\Database\Connection
  31. */
  32. protected $database;
  33. /**
  34. * A user with all the permissions.
  35. *
  36. * @var \Drupal\user\Entity\User
  37. */
  38. protected $privilegedUser;
  39. /**
  40. * A node.
  41. *
  42. * @var \Drupal\node\Entity\Node
  43. */
  44. protected $node;
  45. /**
  46. * A node.
  47. *
  48. * @var \Drupal\node\Entity\Node
  49. */
  50. protected $node2;
  51. protected $defaultSitemapUrl = 'sitemap.xml';
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function setUp() {
  56. parent::setUp();
  57. $this->generator = $this->container->get('simple_sitemap.generator');
  58. $this->database = $this->container->get('database');
  59. $this->drupalCreateContentType(['type' => 'page']);
  60. $this->node = $this->createNode(['title' => 'Node', 'type' => 'page']);
  61. $this->node2 = $this->createNode(['title' => 'Node2', 'type' => 'page']);
  62. // Create a user with all the permissions.
  63. $permissions = array_keys($this->container->get('user.permissions')->getPermissions());
  64. $this->privilegedUser = $this->drupalCreateUser($permissions);
  65. }
  66. /**
  67. * Helper function to replace assertUniqueText.
  68. *
  69. * Also adapt the legacy trait method because it can't be applied to Non-HTML
  70. * pages.
  71. *
  72. * @param string $text
  73. * The text to look for.
  74. *
  75. * @See \Drupal\FunctionalTests\AssertLegacyTrait::assertUniqueText().
  76. */
  77. protected function assertUniqueTextWorkaround($text) {
  78. $page_content = $this->getSession()->getPage()->getContent();
  79. $nr_found = substr_count($page_content, $text);
  80. $this->assertSame(1, $nr_found);
  81. }
  82. /**
  83. * Helper function to replace assertNoUniqueText.
  84. *
  85. * Also adapt the legacy trait method because it can't be applied to Non-HTML
  86. * pages.
  87. *
  88. * @param string $text
  89. * The text to look for.
  90. *
  91. * @See \Drupal\FunctionalTests\AssertLegacyTrait::assertNoUniqueText().
  92. */
  93. protected function assertNoUniqueTextWorkaround($text) {
  94. $page_text = $this->getSession()->getPage()->getContent();
  95. $nr_found = substr_count($page_text, $text);
  96. $this->assertGreaterThan(1, $nr_found);
  97. }
  98. protected function addLanguages($langcodes = 'de') {
  99. foreach ((array) $langcodes as $langcode) {
  100. ConfigurableLanguage::createFromLangcode($langcode)
  101. ->save();
  102. }
  103. }
  104. }