function NodeLoadMultipleTest::testNodeMultipleLoadCaseSensitiveFalse

Same name in other branches
  1. 9 core/modules/node/tests/src/Kernel/NodeLoadMultipleTest.php \Drupal\Tests\node\Kernel\NodeLoadMultipleTest::testNodeMultipleLoadCaseSensitiveFalse()
  2. 8.9.x core/modules/node/tests/src/Functional/NodeLoadMultipleTest.php \Drupal\Tests\node\Functional\NodeLoadMultipleTest::testNodeMultipleLoadCaseSensitiveFalse()
  3. 10 core/modules/node/tests/src/Kernel/NodeLoadMultipleTest.php \Drupal\Tests\node\Kernel\NodeLoadMultipleTest::testNodeMultipleLoadCaseSensitiveFalse()

Creates four nodes with not case sensitive fields and load them.

File

core/modules/node/tests/src/Kernel/NodeLoadMultipleTest.php, line 62

Class

NodeLoadMultipleTest
Tests the loading of multiple nodes.

Namespace

Drupal\Tests\node\Kernel

Code

public function testNodeMultipleLoadCaseSensitiveFalse() : void {
    $field_first_storage = FieldStorageConfig::create([
        'field_name' => 'field_first',
        'entity_type' => 'node',
        'type' => 'string',
        'settings' => [
            'case_sensitive' => FALSE,
        ],
    ]);
    $field_first_storage->save();
    FieldConfig::create([
        'field_storage' => $field_first_storage,
        'bundle' => 'page',
    ])->save();
    $field_second_storage = FieldStorageConfig::create([
        'field_name' => 'field_second',
        'entity_type' => 'node',
        'type' => 'string',
        'settings' => [
            'case_sensitive' => FALSE,
        ],
    ]);
    $field_second_storage->save();
    FieldConfig::create([
        'field_storage' => $field_second_storage,
        'bundle' => 'page',
    ])->save();
    // Test create nodes with values for field_first and field_second.
    $node1 = $this->drupalCreateNode([
        'type' => 'page',
        'field_first' => '1234',
        'field_second' => 'test_value_1',
    ]);
    $this->drupalCreateNode([
        'type' => 'page',
        'field_first' => '1234',
        'field_second' => 'test_value_2',
    ]);
    $node3 = $this->drupalCreateNode([
        'type' => 'page',
        'field_first' => '5678',
        'field_second' => 'test_value_1',
    ]);
    $this->drupalCreateNode([
        'type' => 'page',
        'field_first' => '5678',
        'field_second' => 'test_value_2',
    ]);
    // Load nodes by two properties (field_first and field_second).
    $nodes = $this->container
        ->get('entity_type.manager')
        ->getStorage('node')
        ->loadByProperties([
        'field_first' => [
            '1234',
            '5678',
        ],
        'field_second' => 'test_value_1',
    ]);
    $this->assertCount(2, $nodes);
    $this->assertEquals($node1->field_first->value, $nodes[$node1->id()]->field_first->value);
    $this->assertEquals($node1->field_second->value, $nodes[$node1->id()]->field_second->value);
    $this->assertEquals($node3->field_first->value, $nodes[$node3->id()]->field_first->value);
    $this->assertEquals($node3->field_second->value, $nodes[$node3->id()]->field_second->value);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.