PageRenderTime 62ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/node/src/Tests/Views/NodeFieldFilterTest.php

https://gitlab.com/reasonat/test8
PHP | 109 lines | 41 code | 19 blank | 49 comment | 2 complexity | 37dd995c3fe85ca3c043979080fe5401 MD5 | raw file
  1. <?php
  2. namespace Drupal\node\Tests\Views;
  3. use Drupal\language\Entity\ConfigurableLanguage;
  4. /**
  5. * Tests node field filters with translations.
  6. *
  7. * @group node
  8. */
  9. class NodeFieldFilterTest extends NodeTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static $modules = array('language');
  14. /**
  15. * Views used by this test.
  16. *
  17. * @var array
  18. */
  19. public static $testViews = array('test_field_filters');
  20. /**
  21. * List of node titles by language.
  22. *
  23. * @var array
  24. */
  25. public $nodeTitles = [];
  26. function setUp() {
  27. parent::setUp();
  28. // Create Page content type.
  29. if ($this->profile != 'standard') {
  30. $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
  31. }
  32. // Add two new languages.
  33. ConfigurableLanguage::createFromLangcode('fr')->save();
  34. ConfigurableLanguage::createFromLangcode('es')->save();
  35. // Set up node titles.
  36. $this->nodeTitles = array(
  37. 'en' => 'Food in Paris',
  38. 'es' => 'Comida en Paris',
  39. 'fr' => 'Nouriture en Paris',
  40. );
  41. // Create node with translations.
  42. $node = $this->drupalCreateNode(['title' => $this->nodeTitles['en'], 'langcode' => 'en', 'type' => 'page', 'body' => [['value' => $this->nodeTitles['en']]]]);
  43. foreach (array('es', 'fr') as $langcode) {
  44. $translation = $node->addTranslation($langcode, ['title' => $this->nodeTitles[$langcode]]);
  45. $translation->body->value = $this->nodeTitles[$langcode];
  46. }
  47. $node->save();
  48. }
  49. /**
  50. * Tests body and title filters.
  51. */
  52. public function testFilters() {
  53. // Test the title filter page, which filters for title contains 'Comida'.
  54. // Should show just the Spanish translation, once.
  55. $this->assertPageCounts('test-title-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida title filter');
  56. // Test the body filter page, which filters for body contains 'Comida'.
  57. // Should show just the Spanish translation, once.
  58. $this->assertPageCounts('test-body-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida body filter');
  59. // Test the title Paris filter page, which filters for title contains
  60. // 'Paris'. Should show each translation once.
  61. $this->assertPageCounts('test-title-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris title filter');
  62. // Test the body Paris filter page, which filters for body contains
  63. // 'Paris'. Should show each translation once.
  64. $this->assertPageCounts('test-body-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris body filter');
  65. }
  66. /**
  67. * Asserts that the given node translation counts are correct.
  68. *
  69. * @param string $path
  70. * Path of the page to test.
  71. * @param array $counts
  72. * Array whose keys are languages, and values are the number of times
  73. * that translation should be shown on the given page.
  74. * @param string $message
  75. * Message suffix to display.
  76. */
  77. protected function assertPageCounts($path, $counts, $message) {
  78. // Disable read more links.
  79. entity_get_display('node', 'page', 'teaser')->removeComponent('links')->save();
  80. // Get the text of the page.
  81. $this->drupalGet($path);
  82. $text = $this->getTextContent();
  83. // Check the counts. Note that the title and body are both shown on the
  84. // page, and they are the same. So the title/body string should appear on
  85. // the page twice as many times as the input count.
  86. foreach ($counts as $langcode => $count) {
  87. $this->assertEqual(substr_count($text, $this->nodeTitles[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
  88. }
  89. }
  90. }