You are here

protected function CommentFieldFilterTest::setUp in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/Views/CommentFieldFilterTest.php \Drupal\Tests\comment\Functional\Views\CommentFieldFilterTest::setUp()

Overrides CommentTestBase::setUp

File

core/modules/comment/tests/src/Functional/Views/CommentFieldFilterTest.php, line 39

Class

CommentFieldFilterTest
Tests comment field filters with translations.

Namespace

Drupal\Tests\comment\Functional\Views

Code

protected function setUp($import_test_views = TRUE) {
  parent::setUp($import_test_views);
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'access comments',
  ]));

  // Add two new languages.
  ConfigurableLanguage::createFromLangcode('fr')
    ->save();
  ConfigurableLanguage::createFromLangcode('es')
    ->save();

  // Set up comment titles.
  $this->commentTitles = [
    'en' => 'Food in Paris',
    'es' => 'Comida en Paris',
    'fr' => 'Nourriture en Paris',
  ];

  // Create a new comment. Using the one created earlier will not work,
  // as it predates the language set-up.
  $comment = [
    'uid' => $this->loggedInUser
      ->id(),
    'entity_id' => $this->nodeUserCommented
      ->id(),
    'entity_type' => 'node',
    'field_name' => 'comment',
    'cid' => '',
    'pid' => '',
    'node_type' => '',
  ];
  $this->comment = Comment::create($comment);

  // Add field values and translate the comment.
  $this->comment->subject->value = $this->commentTitles['en'];
  $this->comment->comment_body->value = $this->commentTitles['en'];
  $this->comment->langcode = 'en';
  $this->comment
    ->save();
  foreach ([
    'es',
    'fr',
  ] as $langcode) {
    $translation = $this->comment
      ->addTranslation($langcode, []);
    $translation->comment_body->value = $this->commentTitles[$langcode];
    $translation->subject->value = $this->commentTitles[$langcode];
  }
  $this->comment
    ->save();
}