Skip to content
Snippets Groups Projects
Select Git revision
  • f8f8eee12241ef3abb58957700cdf3b7ad644558
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

EntityReferenceRelationshipTest.php

Blame
  • Alex Pott's avatar
    Issue #2325691 by jarsenx: Entity Reference Module: Fix documentation that...
    Alex Pott authored
    Issue #2325691 by jarsenx: Entity Reference Module: Fix documentation that refers to enabling/disabling of modules.
    f8f8eee1
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    EntityReferenceRelationshipTest.php 5.26 KiB
    <?php
    
    /**
     * @file
     * Contains \Drupal\entity_reference\Tests\Views\EntityReferenceRelationshipTest.
     */
    
    namespace Drupal\entity_reference\Tests\Views;
    
    use Drupal\Core\Field\FieldStorageDefinitionInterface;
    use Drupal\field\Entity\FieldStorageConfig;
    use Drupal\field\Entity\FieldInstanceConfig;
    use Drupal\views\Tests\ViewTestData;
    use Drupal\views\Tests\ViewUnitTestBase;
    use Drupal\views\Views;
    
    /**
     * Tests entity reference relationship data.
     *
     * @group entity_reference
     * @see entity_reference_field_views_data()
     */
    class EntityReferenceRelationshipTest extends ViewUnitTestBase {
    
      /**
       * Views used by this test.
       *
       * @var array
       */
      public static $testViews = array('test_entity_reference_view');
    
      /**
       * Modules to install.
       *
       * @var array
       */
      public static $modules = array('user', 'field', 'entity_test', 'options', 'entity_reference', 'views', 'entity_reference_test_views');
    
      /**
       * The entity_test entities used by the test.
       *
       * @var array
       */
      protected $entities = array();
    
      /**
       * {@inheritdoc}
       */
      protected function setUp() {
        parent::setUp();
    
        $this->installEntitySchema('entity_test');
    
        ViewTestData::createTestViews(get_class($this), array('entity_reference_test_views'));
    
        $field_storage = FieldStorageConfig::create(array(
          'settings' => array(
            'target_type' => 'entity_test',
          ),
          'entity_type' => 'entity_test',
          'name' => 'field_test',
          'type' => 'entity_reference',
          'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
        ));
        $field_storage->save();
    
        $instance = FieldInstanceConfig::create(array(
          'entity_type' => 'entity_test',
          'field_name' => 'field_test',
          'bundle' => 'entity_test',
          'settings' => array(
            'handler' => 'default',
            'handler_settings' => array(),
          ),
        ));
        $instance->save();
    
        // Create some test entities which link each other.
        $entity_storage= \Drupal::entityManager()->getStorage('entity_test');
        $referenced_entity = $entity_storage->create(array());
        $referenced_entity->save();
        $this->entities[$referenced_entity->id()] = $referenced_entity;
    
        $entity = $entity_storage->create(array());
        $entity->field_test->target_id = $referenced_entity->id();
        $entity->save();
        $this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id());
        $this->entities[$entity->id()] = $entity;
    
        $entity = $entity_storage->create(array());
        $entity->field_test->target_id = $referenced_entity->id();
        $entity->save();
        $this->assertEqual($entity->field_test[0]->entity->id(), $referenced_entity->id());
        $this->entities[$entity->id()] = $entity;
      }
    
      /**
       * Tests using the views relationship.
       */
      public function testRelationship() {
        // Check just the generated views data.
        $views_data_field_test = Views::viewsData()->get('entity_test__field_test');
        $this->assertEqual($views_data_field_test['field_test']['relationship']['id'], 'standard');
        $this->assertEqual($views_data_field_test['field_test']['relationship']['base'], 'entity_test');
        $this->assertEqual($views_data_field_test['field_test']['relationship']['base field'], 'id');
        $this->assertEqual($views_data_field_test['field_test']['relationship']['relationship field'], 'field_test_target_id');
    
        // Check the backwards reference.
        $views_data_entity_test = Views::viewsData()->get('entity_test');
        $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['id'], 'entity_reverse');
        $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['base'], 'entity_test');
        $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['base field'], 'id');
        $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['field table'], 'entity_test__field_test');
        $this->assertEqual($views_data_entity_test['reverse__entity_test__field_test']['relationship']['field field'], 'field_test_target_id');
    
    
        // Check an actual test view.
        $view = Views::getView('test_entity_reference_view');
        $this->executeView($view);
    
        foreach (array_keys($view->result) as $index) {
          // Just check that the actual ID of the entity is the expected one.
          $this->assertEqual($view->result[$index]->id, $this->entities[$index + 1]->id());
          // Test the forward relationship.
          // The second and third entity refer to the first one.
          // The value key on the result will be in the format
          // BASE_TABLE_FIELD_NAME.
          $this->assertEqual($view->result[$index]->entity_test_entity_test__field_test_id, $index == 0 ? NULL : 1);
    
          if ($index > 0) {
            // Test that the correct relationship entity is on the row.
            $this->assertEqual($view->result[$index]->_relationship_entities['test_relationship']->id(), 1);
          }
        }
    
        $view->destroy();
        $this->executeView($view, 'embed_1');
    
        foreach (array_keys($view->result) as $index) {
          $this->assertEqual($view->result[$index]->id, $this->entities[$index + 1]->id());
          // The second and third entity refer to the first one.
          $this->assertEqual($view->result[$index]->entity_test_entity_test__field_test_id, $index == 0 ? NULL : 1);
        }
      }
    
    }