You are here

function ContentTmgmtEntitySourceListTest::testEntitySourceListSearch in Translation Management Tool 8

File

sources/content/tests/src/Functional/ContentTmgmtEntitySourceListTest.php, line 353

Class

ContentTmgmtEntitySourceListTest
Tests the user interface for entity translation lists.

Namespace

Drupal\Tests\tmgmt_content\Functional

Code

function testEntitySourceListSearch() {

  // We need a node with title composed of several words to test
  // "any words" search.
  $title_part_1 = $this
    ->randomMachineName('4');
  $title_part_2 = $this
    ->randomMachineName('4');
  $title_part_3 = $this
    ->randomMachineName('4');
  $this->nodes['article']['en'][0]->title = "{$title_part_1} {$title_part_2} {$title_part_3}";
  $this->nodes['article']['en'][0]
    ->save();

  // Submit partial node title and see if we have a result.
  $edit = array();
  $edit['search[title]'] = "{$title_part_1} {$title_part_3}";
  $this
    ->drupalPostForm('admin/tmgmt/sources/content/node', $edit, t('Search'));
  $this
    ->assertSession()
    ->pageTextContains("{$title_part_1} {$title_part_2} {$title_part_3}", 'Searching on partial node title must return the result.');

  // Check if there is only one result in the list.
  $search_result_rows = $this
    ->xpath('//table[@id="edit-items"]/tbody/tr');
  $this
    ->assert(count($search_result_rows) == 1, 'The search result must return only one row.');

  // To test if other entity types work go for simple comment search.
  $this
    ->addDefaultCommentField('node', 'article');
  $content_translation_manager = \Drupal::service('content_translation.manager');
  $content_translation_manager
    ->setEnabled('comment', 'comment', TRUE);
  $values = array(
    'entity_type' => 'node',
    'entity_id' => $this->nodes['article']['en'][0]
      ->id(),
    'field_name' => 'comment',
    'comment_type' => 'comment',
    'comment_body' => $this
      ->randomMachineName(),
    'subject' => $this
      ->randomMachineName(),
  );
  $comment = Comment::create($values);
  $comment
    ->save();

  // Do search for the comment.
  $edit = array();
  $edit['search[subject]'] = $comment
    ->getSubject();
  $this
    ->drupalPostForm('admin/tmgmt/sources/content/comment', $edit, t('Search'));
  $this
    ->assertSession()
    ->pageTextContains($comment
    ->getSubject(), 'Searching for a comment subject.');

  // Tests that search bundle filter works.
  $this
    ->drupalPostForm('/admin/tmgmt/sources/content/node', [
    'search[title]' => $this->nodes['article']['en'][0]
      ->label(),
  ], 'Search');
  $this
    ->assertSession()
    ->pageTextContains(t('Content overview'));
  $this
    ->assertSession()
    ->pageTextContains($this->nodes['article']['en'][0]
    ->label());
  $this
    ->drupalPostForm('/admin/tmgmt/sources/content/node', [
    'search[title]' => 'wrong_value',
  ], 'Search');
  $this
    ->assertSession()
    ->pageTextContains(t('Content overview'));
  $this
    ->assertSession()
    ->pageTextNotContains($this->nodes['article']['en'][0]
    ->label());
  $options = [
    'query' => [
      'any_key' => 'any_value',
    ],
  ];
  $this
    ->drupalGet('/admin/tmgmt/sources/content/node', $options);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains($this->nodes['article']['en'][0]
    ->label());

  // Test combined title and language filter.
  $this
    ->drupalGet('/admin/tmgmt/sources/content/node');
  $edit = [
    'search[target_language]' => 'de',
    'search[title]' => $this->nodes['article']['en'][0]
      ->label(),
  ];
  $this
    ->submitForm($edit, 'Search');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->linkExists($this->nodes['article']['en'][0]
    ->label());
}