function SqlTest::testLoadEntitiesWithNonEntityRelationship

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Unit/Plugin/query/SqlTest.php \Drupal\Tests\views\Unit\Plugin\query\SqlTest::testLoadEntitiesWithNonEntityRelationship()
  2. 8.9.x core/modules/views/tests/src/Unit/Plugin/query/SqlTest.php \Drupal\Tests\views\Unit\Plugin\query\SqlTest::testLoadEntitiesWithNonEntityRelationship()
  3. 10 core/modules/views/tests/src/Unit/Plugin/query/SqlTest.php \Drupal\Tests\views\Unit\Plugin\query\SqlTest::testLoadEntitiesWithNonEntityRelationship()

@covers ::loadEntities @covers ::assignEntitiesToResult

File

core/modules/views/tests/src/Unit/Plugin/query/SqlTest.php, line 393

Class

SqlTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21views%21src%21Plugin%21views%21query%21Sql.php/class/Sql/11.x" title="Views query plugin for an SQL query." class="local">\Drupal\views\Plugin\views\query\Sql</a>

Namespace

Drupal\Tests\views\Unit\Plugin\query

Code

public function testLoadEntitiesWithNonEntityRelationship() : void {
    // We don't use prophecy, because prophecy enforces methods.
    $view = $this->getMockBuilder(ViewExecutable::class)
        ->disableOriginalConstructor()
        ->getMock();
    $this->setupViewWithRelationships($view, 'entity_first_field_data');
    $view_entity = $this->prophesize(ViewEntityInterface::class);
    $view_entity->get('base_table')
        ->willReturn('entity_first');
    $view_entity->get('base_field')
        ->willReturn('id');
    $view->storage = $view_entity->reveal();
    $entities = [
        'first' => [
            1 => $this->prophesize(EntityInterface::class)
                ->reveal(),
            2 => $this->prophesize(EntityInterface::class)
                ->reveal(),
        ],
    ];
    $entity_type_manager = $this->setupEntityTypes($entities);
    $date_sql = $this->prophesize(DateSqlInterface::class);
    $messenger = $this->prophesize(MessengerInterface::class);
    $query = new Sql([], 'sql', [], $entity_type_manager->reveal(), $date_sql->reveal(), $messenger->reveal());
    $query->view = $view;
    $result = [];
    $result[] = new ResultRow([
        'id' => 1,
    ]);
    $result[] = new ResultRow([
        'id' => 2,
    ]);
    $query->addField('entity_first', 'id', 'id');
    $query->loadEntities($result);
    $entity_information = $query->getEntityTableInfo();
    $this->assertSame($entities['first'][1], $result[0]->_entity);
    $this->assertSame($entities['first'][2], $result[1]->_entity);
    $this->assertEquals([], $result[0]->_relationship_entities);
    $this->assertEquals([], $result[1]->_relationship_entities);
    // This is an entity table and should be in $entity_information.
    $this->assertContains('first', array_keys($entity_information));
    // This is not an entity table and should not be in $entity_information.
    $this->assertNotContains('entity_first_field_data__entity_first_field_data', array_keys($entity_information));
}

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