SortTranslationTest.php 3.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 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 43 44
<?php

namespace Drupal\Tests\views\Kernel\Handler;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;

/**
 * Tests sorting on translatable and not translatable fields.
 *
 * @group views
 */
class SortTranslationTest extends ViewsKernelTestBase {
  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'node',
    'field',
    'language',
  ];

  /**
   * {@inheritdoc}
   */
  public static $testViews = [
    'test_view_sort_translation',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
    ConfigurableLanguage::createFromLangcode('de')->save();
    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');

45
    // $this->installConfig('node');
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    $this->container->get('kernel')->rebuildContainer();

    $node_type = NodeType::create(['type' => 'article']);
    $node_type->save();

    FieldStorageConfig::create([
      'field_name' => 'text',
      'entity_type' => 'node',
      'type' => 'string',
    ])->save();

    FieldConfig::create([
      'field_name' => 'text',
      'entity_type' => 'node',
      'bundle' => 'article',
      'label' => 'Translated text',
      'translatable' => TRUE
    ])->save();

    FieldStorageConfig::create([
      'field_name' => 'weight',
      'entity_type' => 'node',
      'type' => 'integer',
    ])->save();

    FieldConfig::create([
      'field_name' => 'weight',
      'entity_type' => 'node',
      'bundle' => 'article',
      'translatable' => FALSE,
    ])->save();

    for ($i = 0; $i < 3; $i++) {
      $node = Node::create([
        'type' => 'article',
        'title' => 'Title en ' . $i,
        'weight' => ['value' => 3 - $i],
        'text' => ['value' => 'moo en ' . $i],
        'langcode' => 'en',
      ]);
      $node->save();

      $translation = $node->addTranslation('de');
      $translation->title->value = 'Title DE ' . $i;
      $translation->text->value = 'moo DE ' . $i;
      $translation->save();
      $nodes[] = $node;
    }
  }

  /**
   * Test sorting on an untranslated field.
   */
  public function testSortbyUntranslatedIntegerField() {
    $map = [
      'nid' => 'nid',
      'node_field_data_langcode' => 'langcode',
    ];

    $view = Views::getView('test_view_sort_translation');
    $view->setDisplay('default');
    $this->executeView($view);

    // With ascending sort, the nodes should come out in reverse order.
    $expected = [
      [
        'nid' => 3,
        'langcode' => 'en',
      ],
      [
        'nid' => 2,
        'langcode' => 'en',
      ],
      [
        'nid' => 1,
        'langcode' => 'en',
      ],
    ];
    $this->assertIdenticalResultset($view, $expected, $map);
    $view->destroy();

    $view = Views::getView('test_view_sort_translation');
    $view->setDisplay('display_de');
    $this->executeView($view);

    $expected = [
      [
        'nid' => 3,
        'langcode' => 'de',
      ],
      [
        'nid' => 2,
        'langcode' => 'de',
      ],
      [
        'nid' => 1,
        'langcode' => 'de',
      ],
    ];

    // The weight field is not translated, we sort by it so the nodes
    // should come out in the same order in both languages.
    $this->assertIdenticalResultset($view, $expected, $map);
    $view->destroy();
  }

}