You are here

protected function FieldFieldTest::setUp in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/FieldFieldTest.php \Drupal\Tests\views\Kernel\Handler\FieldFieldTest::setUp()
  2. 10 core/modules/views/tests/src/Kernel/Handler/FieldFieldTest.php \Drupal\Tests\views\Kernel\Handler\FieldFieldTest::setUp()

Parameters

bool $import_test_views: Should the views specified on the test class be imported. If you need to setup some additional stuff, like fields, you need to call false and then call createTestViews for your own.

Overrides ViewsKernelTestBase::setUp

File

core/modules/views/tests/src/Kernel/Handler/FieldFieldTest.php, line 71

Class

FieldFieldTest
Provides some integration tests for the Field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

protected function setUp($import_test_views = TRUE) {

  // First setup the needed entity types before installing the views.
  parent::setUp(FALSE);
  $this
    ->installEntitySchema('user');
  $this
    ->installEntitySchema('entity_test');
  $this
    ->installEntitySchema('entity_test_rev');
  ViewTestData::createTestViews(get_class($this), [
    'views_test_config',
  ]);

  // Bypass any field access.
  $this->adminUser = User::create([
    'name' => $this
      ->randomString(),
  ]);
  $this->adminUser
    ->save();
  $this->container
    ->get('current_user')
    ->setAccount($this->adminUser);
  $this->testUsers = [];
  for ($i = 0; $i < 5; $i++) {
    $this->testUsers[$i] = User::create([
      'name' => 'test ' . $i,
      'timezone' => User::getAllowedTimezones()[$i],
      'created' => REQUEST_TIME - rand(0, 3600),
    ]);
    $this->testUsers[$i]
      ->save();
  }

  // Setup a field storage and field, but also change the views data for the
  // entity_test entity type.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_test',
    'type' => 'integer',
    'entity_type' => 'entity_test',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'field_test',
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  ]);
  $field
    ->save();
  $field_storage_multiple = FieldStorageConfig::create([
    'field_name' => 'field_test_multiple',
    'type' => 'integer',
    'entity_type' => 'entity_test',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_storage_multiple
    ->save();
  $field_multiple = FieldConfig::create([
    'field_name' => 'field_test_multiple',
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  ]);
  $field_multiple
    ->save();
  $random_number = (string) 30856;
  $random_number_multiple = (string) 1370359990;
  for ($i = 0; $i < 5; $i++) {
    $this->entities[$i] = $entity = EntityTest::create([
      'bundle' => 'entity_test',
      'name' => 'test ' . $i,
      'field_test' => $random_number[$i],
      'field_test_multiple' => [
        $random_number_multiple[$i * 2],
        $random_number_multiple[$i * 2 + 1],
      ],
      'user_id' => $this->testUsers[$i]
        ->id(),
    ]);
    $entity
      ->save();
  }

  // Setup some test data for entities with revisions.
  // We are testing both base field revisions and field config revisions.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_test',
    'type' => 'integer',
    'entity_type' => 'entity_test_rev',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'field_test',
    'entity_type' => 'entity_test_rev',
    'bundle' => 'entity_test_rev',
  ]);
  $field
    ->save();
  $field_storage_multiple = FieldStorageConfig::create([
    'field_name' => 'field_test_multiple',
    'type' => 'integer',
    'entity_type' => 'entity_test_rev',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_storage_multiple
    ->save();
  $field_multiple = FieldConfig::create([
    'field_name' => 'field_test_multiple',
    'entity_type' => 'entity_test_rev',
    'bundle' => 'entity_test_rev',
  ]);
  $field_multiple
    ->save();
  $this->entityRevision = [];
  $this->entityRevision[0] = $entity = EntityTestRev::create([
    'name' => 'base value',
    'field_test' => 1,
    'field_test_multiple' => [
      1,
      3,
      7,
    ],
    'user_id' => $this->testUsers[0]
      ->id(),
  ]);
  $entity
    ->save();
  $original_entity = clone $entity;
  $entity = clone $original_entity;
  $entity
    ->setNewRevision(TRUE);
  $entity->name->value = 'revision value1';
  $entity->field_test->value = 2;
  $entity->field_test_multiple[0]->value = 0;
  $entity->field_test_multiple[1]->value = 3;
  $entity->field_test_multiple[2]->value = 5;
  $entity->user_id->target_id = $this->testUsers[1]
    ->id();
  $entity
    ->save();
  $this->entityRevision[1] = $entity;
  $entity = clone $original_entity;
  $entity
    ->setNewRevision(TRUE);
  $entity->name->value = 'revision value2';
  $entity->field_test->value = 3;
  $entity->field_test_multiple[0]->value = 9;
  $entity->field_test_multiple[1]->value = 9;
  $entity->field_test_multiple[2]->value = 9;
  $entity->user_id->target_id = $this->testUsers[2]
    ->id();
  $entity
    ->save();
  $this->entityRevision[2] = $entity;
  $this->entityRevision[3] = $entity = EntityTestRev::create([
    'name' => 'next entity value',
    'field_test' => 4,
    'field_test_multiple' => [
      2,
      9,
      9,
    ],
    'user_id' => $this->testUsers[3]
      ->id(),
  ]);
  $entity
    ->save();
  \Drupal::state()
    ->set('entity_test.views_data', [
    'entity_test' => [
      'id' => [
        'field' => [
          'id' => 'field',
        ],
      ],
    ],
    'entity_test_rev_revision' => [
      'id' => [
        'field' => [
          'id' => 'field',
        ],
      ],
    ],
  ]);
  Views::viewsData()
    ->clear();
}