CommentFieldFilterTest.php 4.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\comment\Tests\Views\CommentFieldFilterTest.
 */

namespace Drupal\comment\Tests\Views;

use Drupal\field\Entity\FieldStorageConfig;
11
use Drupal\language\Entity\ConfigurableLanguage;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/**
 * Tests comment field filters with translations.
 *
 * @group comment
 */
class CommentFieldFilterTest extends CommentTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = array('language');

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_field_filters');

  /**
   * List of comment titles by language.
   *
   * @var array
   */
  public $comment_titles = array();

  function setUp() {
    parent::setUp();

    // Add two new languages.
43 44
    ConfigurableLanguage::createFromLangcode('fr')->save();
    ConfigurableLanguage::createFromLangcode('es')->save();
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

    // Make the comment body field translatable. The title is already
    // translatable by definition.
    $field = FieldStorageConfig::loadByName('comment', 'comment_body');
    $field->translatable = TRUE;
    $field->save();

    // Set up comment titles.
    $this->comment_titles = array(
      'en' => 'Food in Paris',
      'es' => 'Comida en Paris',
      'fr' => 'Nouriture en Paris',
    );

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

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

  /**
   * Tests body and title filters.
   */
  public function testFilters() {
    // Test the title filter page, which filters for title contains 'Comida'.
    // Should show just the Spanish translation, once.
    $this->assertPageCounts('test-title-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida title filter');

    // Test the body filter page, which filters for body contains 'Comida'.
    // Should show just the Spanish translation, once.
    $this->assertPageCounts('test-body-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida body filter');

    // Test the title Paris filter page, which filters for title contains
    // 'Paris'. Should show each translation once.
    $this->assertPageCounts('test-title-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris title filter');

    // Test the body Paris filter page, which filters for body contains
    // 'Paris'. Should show each translation once.
    $this->assertPageCounts('test-body-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris body filter');
  }

  /**
   * Asserts that the given comment translation counts are correct.
   *
   * @param string $path
   *   Path of the page to test.
   * @param array $counts
   *   Array whose keys are languages, and values are the number of times
   *   that translation should be shown on the given page.
   * @param string $message
   *   Message suffix to display.
   */
  protected function assertPageCounts($path, $counts, $message) {
    // Get the text of the page.
    $this->drupalGet($path);
    $text = $this->getTextContent();

    // Check the counts. Note that the title and body are both shown on the
    // page, and they are the same. So the title/body string should appear on
    // the page twice as many times as the input count.
    foreach ($counts as $langcode => $count) {
      $this->assertEqual(substr_count($text, $this->comment_titles[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
    }
  }
}