You are here

class EntityReferenceItemTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest

Tests the new entity API for the entity reference field type.

@group entity_reference

Hierarchy

Expanded class hierarchy of EntityReferenceItemTest

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php, line 33

Namespace

Drupal\Tests\field\Kernel\EntityReference
View source
class EntityReferenceItemTest extends FieldKernelTestBase {
  use EntityReferenceTestTrait;

  /**
   * Modules to install.
   *
   * @var array
   */
  protected static $modules = [
    'node',
    'comment',
    'file',
    'taxonomy',
    'text',
    'filter',
    'views',
    'field',
  ];

  /**
   * The taxonomy vocabulary to test with.
   *
   * @var \Drupal\taxonomy\VocabularyInterface
   */
  protected $vocabulary;

  /**
   * The taxonomy term to test with.
   *
   * @var \Drupal\taxonomy\TermInterface
   */
  protected $term;

  /**
   * The test entity with a string ID.
   *
   * @var \Drupal\entity_test\Entity\EntityTestStringId
   */
  protected $entityStringId;

  /**
   * Sets up the test.
   */
  protected function setUp() : void {
    parent::setUp();
    $this
      ->installEntitySchema('entity_test_string_id');
    $this
      ->installEntitySchema('taxonomy_term');
    $this
      ->installEntitySchema('node');
    $this
      ->installEntitySchema('comment');
    $this
      ->installEntitySchema('file');
    $this
      ->installSchema('comment', [
      'comment_entity_statistics',
    ]);
    $this
      ->installSchema('node', [
      'node_access',
    ]);
    $this->vocabulary = Vocabulary::create([
      'name' => $this
        ->randomMachineName(),
      'vid' => mb_strtolower($this
        ->randomMachineName()),
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $this->vocabulary
      ->save();
    $this->term = Term::create([
      'name' => $this
        ->randomMachineName(),
      'vid' => $this->vocabulary
        ->id(),
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $this->term
      ->save();
    NodeType::create([
      'type' => $this
        ->randomMachineName(),
    ])
      ->save();
    CommentType::create([
      'id' => $this
        ->randomMachineName(),
      'target_entity_type_id' => 'node',
    ])
      ->save();
    $this->entityStringId = EntityTestStringId::create([
      'id' => $this
        ->randomMachineName(),
    ]);
    $this->entityStringId
      ->save();

    // Use the util to create an instance.
    $this
      ->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term');
    $this
      ->createEntityReferenceField('entity_test', 'entity_test', 'field_test_entity_test_string_id', 'Test content entity reference with string ID', 'entity_test_string_id');
    $this
      ->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary');
    $this
      ->createEntityReferenceField('entity_test', 'entity_test', 'field_test_node', 'Test node entity reference', 'node', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $this
      ->createEntityReferenceField('entity_test', 'entity_test', 'field_test_user', 'Test user entity reference', 'user');
    $this
      ->createEntityReferenceField('entity_test', 'entity_test', 'field_test_comment', 'Test comment entity reference', 'comment');
    $this
      ->createEntityReferenceField('entity_test', 'entity_test', 'field_test_file', 'Test file entity reference', 'file');
    $this
      ->createEntityReferenceField('entity_test_string_id', 'entity_test_string_id', 'field_test_entity_test', 'Test content entity reference with string ID', 'entity_test');
  }

  /**
   * Tests the entity reference field type for referencing content entities.
   */
  public function testContentEntityReferenceItem() {
    $tid = $this->term
      ->id();

    // Just being able to create the entity like this verifies a lot of code.
    $entity = EntityTest::create();
    $entity->field_test_taxonomy_term->target_id = $tid;
    $entity->name->value = $this
      ->randomMachineName();
    $entity
      ->save();
    $entity = EntityTest::load($entity
      ->id());
    $this
      ->assertInstanceOf(FieldItemListInterface::class, $entity->field_test_taxonomy_term);
    $this
      ->assertInstanceOf(FieldItemInterface::class, $entity->field_test_taxonomy_term[0]);
    $this
      ->assertEquals($tid, $entity->field_test_taxonomy_term->target_id);
    $this
      ->assertEquals($this->term
      ->getName(), $entity->field_test_taxonomy_term->entity
      ->getName());
    $this
      ->assertEquals($tid, $entity->field_test_taxonomy_term->entity
      ->id());
    $this
      ->assertEquals($this->term
      ->uuid(), $entity->field_test_taxonomy_term->entity
      ->uuid());

    // Verify that the label for the target ID property definition is correct.
    $label = $entity->field_test_taxonomy_term
      ->getFieldDefinition()
      ->getFieldStorageDefinition()
      ->getPropertyDefinition('target_id')
      ->getLabel();
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $label);
    $this
      ->assertEquals('Taxonomy term ID', $label
      ->render());

    // Change the name of the term via the reference.
    $new_name = $this
      ->randomMachineName();
    $entity->field_test_taxonomy_term->entity
      ->setName($new_name);
    $entity->field_test_taxonomy_term->entity
      ->save();

    // Verify it is the correct name.
    $term = Term::load($tid);
    $this
      ->assertEquals($new_name, $term
      ->getName());

    // Make sure the computed term reflects updates to the term id.
    $term2 = Term::create([
      'name' => $this
        ->randomMachineName(),
      'vid' => $this->term
        ->bundle(),
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $term2
      ->save();

    // Test all the possible ways of assigning a value.
    $entity->field_test_taxonomy_term->target_id = $term
      ->id();
    $this
      ->assertEquals($term
      ->id(), $entity->field_test_taxonomy_term->entity
      ->id());
    $this
      ->assertEquals($term
      ->getName(), $entity->field_test_taxonomy_term->entity
      ->getName());
    $entity->field_test_taxonomy_term = [
      [
        'target_id' => $term2
          ->id(),
      ],
    ];
    $this
      ->assertEquals($term2
      ->id(), $entity->field_test_taxonomy_term->entity
      ->id());
    $this
      ->assertEquals($term2
      ->getName(), $entity->field_test_taxonomy_term->entity
      ->getName());

    // Test value assignment via the computed 'entity' property.
    $entity->field_test_taxonomy_term->entity = $term;
    $this
      ->assertEquals($term
      ->id(), $entity->field_test_taxonomy_term->target_id);
    $this
      ->assertEquals($term
      ->getName(), $entity->field_test_taxonomy_term->entity
      ->getName());
    $entity->field_test_taxonomy_term = [
      [
        'entity' => $term2,
      ],
    ];
    $this
      ->assertEquals($term2
      ->id(), $entity->field_test_taxonomy_term->target_id);
    $this
      ->assertEquals($term2
      ->getName(), $entity->field_test_taxonomy_term->entity
      ->getName());

    // Test assigning an invalid item throws an exception.
    try {
      $entity->field_test_taxonomy_term = [
        'target_id' => 'invalid',
        'entity' => $term2,
      ];
      $this
        ->fail('Assigning an invalid item throws an exception.');
    } catch (\Exception $e) {
      $this
        ->assertInstanceOf(\InvalidArgumentException::class, $e);
    }

    // Delete terms so we have nothing to reference and try again
    $term
      ->delete();
    $term2
      ->delete();
    $entity = EntityTest::create([
      'name' => $this
        ->randomMachineName(),
    ]);
    $entity
      ->save();

    // Test the generateSampleValue() method.
    $entity = EntityTest::create();
    $entity->field_test_taxonomy_term
      ->generateSampleItems();
    $entity->field_test_taxonomy_vocabulary
      ->generateSampleItems();
    $this
      ->entityValidateAndSave($entity);

    // Tests that setting an integer target ID together with an entity object
    // succeeds and does not cause any exceptions. There is no assertion here,
    // as the assignment should not throw any exceptions and if it does the
    // test will fail.
    // @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::setValue().
    $user = User::create([
      'name' => $this
        ->randomString(),
    ]);
    $user
      ->save();
    $entity = EntityTest::create([
      'user_id' => [
        'target_id' => (int) $user
          ->id(),
        'entity' => $user,
      ],
    ]);
  }

  /**
   * Tests the ::generateSampleValue() method.
   */
  public function testGenerateSampleValue() {
    $entity = EntityTest::create();

    // Test while a term exists.
    $entity->field_test_taxonomy_term
      ->generateSampleItems();
    $this
      ->assertInstanceOf(TermInterface::class, $entity->field_test_taxonomy_term->entity);
    $this
      ->entityValidateAndSave($entity);

    // Delete the term and test again.
    $this->term
      ->delete();
    $entity->field_test_taxonomy_term
      ->generateSampleItems();
    $this
      ->assertInstanceOf(TermInterface::class, $entity->field_test_taxonomy_term->entity);
    $this
      ->entityValidateAndSave($entity);
  }

  /**
   * Tests the ::generateSampleValue() method when it has a circular reference.
   */
  public function testGenerateSampleValueCircularReference() {

    // Delete the existing entity.
    $this->entityStringId
      ->delete();
    $entity_storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test');
    $entity = $entity_storage
      ->createWithSampleValues('entity_test');
    $this
      ->assertInstanceOf(EntityTestStringId::class, $entity->field_test_entity_test_string_id->entity);
    $this
      ->assertInstanceOf(EntityTest::class, $entity->field_test_entity_test_string_id->entity->field_test_entity_test->entity);
  }

  /**
   * Tests referencing content entities with string IDs.
   */
  public function testContentEntityReferenceItemWithStringId() {
    $entity = EntityTest::create();
    $entity->field_test_entity_test_string_id->target_id = $this->entityStringId
      ->id();
    $entity
      ->save();
    $storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test');
    $storage
      ->resetCache();
    $this
      ->assertEquals($this->entityStringId
      ->id(), $storage
      ->load($entity
      ->id())->field_test_entity_test_string_id->target_id);

    // Verify that the label for the target ID property definition is correct.
    $label = $entity->field_test_taxonomy_term
      ->getFieldDefinition()
      ->getFieldStorageDefinition()
      ->getPropertyDefinition('target_id')
      ->getLabel();
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $label);
    $this
      ->assertEquals('Taxonomy term ID', $label
      ->render());
  }

  /**
   * Tests the entity reference field type for referencing config entities.
   */
  public function testConfigEntityReferenceItem() {
    $referenced_entity_id = $this->vocabulary
      ->id();

    // Just being able to create the entity like this verifies a lot of code.
    $entity = EntityTest::create();
    $entity->field_test_taxonomy_vocabulary->target_id = $referenced_entity_id;
    $entity->name->value = $this
      ->randomMachineName();
    $entity
      ->save();
    $entity = EntityTest::load($entity
      ->id());
    $this
      ->assertInstanceOf(FieldItemListInterface::class, $entity->field_test_taxonomy_vocabulary);
    $this
      ->assertInstanceOf(FieldItemInterface::class, $entity->field_test_taxonomy_vocabulary[0]);
    $this
      ->assertEquals($referenced_entity_id, $entity->field_test_taxonomy_vocabulary->target_id);
    $this
      ->assertEquals($this->vocabulary
      ->label(), $entity->field_test_taxonomy_vocabulary->entity
      ->label());
    $this
      ->assertEquals($referenced_entity_id, $entity->field_test_taxonomy_vocabulary->entity
      ->id());
    $this
      ->assertEquals($this->vocabulary
      ->uuid(), $entity->field_test_taxonomy_vocabulary->entity
      ->uuid());

    // Change the name of the term via the reference.
    $new_name = $this
      ->randomMachineName();
    $entity->field_test_taxonomy_vocabulary->entity
      ->set('name', $new_name);
    $entity->field_test_taxonomy_vocabulary->entity
      ->save();

    // Verify it is the correct name.
    $vocabulary = Vocabulary::load($referenced_entity_id);
    $this
      ->assertEquals($new_name, $vocabulary
      ->label());

    // Make sure the computed term reflects updates to the term id.
    $vocabulary2 = $vocabulary = Vocabulary::create([
      'name' => $this
        ->randomMachineName(),
      'vid' => mb_strtolower($this
        ->randomMachineName()),
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $vocabulary2
      ->save();
    $entity->field_test_taxonomy_vocabulary->target_id = $vocabulary2
      ->id();
    $this
      ->assertEquals($vocabulary2
      ->id(), $entity->field_test_taxonomy_vocabulary->entity
      ->id());
    $this
      ->assertEquals($vocabulary2
      ->label(), $entity->field_test_taxonomy_vocabulary->entity
      ->label());

    // Delete terms so we have nothing to reference and try again
    $this->vocabulary
      ->delete();
    $vocabulary2
      ->delete();
    $entity = EntityTest::create([
      'name' => $this
        ->randomMachineName(),
    ]);
    $entity
      ->save();
  }

  /**
   * Tests entity auto create.
   */
  public function testEntityAutoCreate() {

    // The term entity is unsaved here.
    $term = Term::create([
      'name' => $this
        ->randomMachineName(),
      'vid' => $this->term
        ->bundle(),
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $entity = EntityTest::create();

    // Now assign the unsaved term to the field.
    $entity->field_test_taxonomy_term->entity = $term;
    $entity->name->value = $this
      ->randomMachineName();

    // This is equal to storing an entity to tempstore or cache and retrieving
    // it back. An example for this is node preview.
    $entity = serialize($entity);
    $entity = unserialize($entity);

    // And then the entity.
    $entity
      ->save();
    $term = \Drupal::service('entity.repository')
      ->loadEntityByUuid($term
      ->getEntityTypeId(), $term
      ->uuid());
    $this
      ->assertEquals($entity->field_test_taxonomy_term->entity
      ->id(), $term
      ->id());
  }

  /**
   * Tests saving order sequence doesn't matter.
   */
  public function testEntitySaveOrder() {

    // The term entity is unsaved here.
    $term = Term::create([
      'name' => $this
        ->randomMachineName(),
      'vid' => $this->term
        ->bundle(),
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $entity = EntityTest::create();

    // Now assign the unsaved term to the field.
    $entity->field_test_taxonomy_term->entity = $term;
    $entity->name->value = $this
      ->randomMachineName();

    // Now get the field value.
    $value = $entity
      ->get('field_test_taxonomy_term');
    $this
      ->assertTrue(empty($value['target_id']));
    $this
      ->assertNull($entity->field_test_taxonomy_term->target_id);

    // And then set it.
    $entity->field_test_taxonomy_term = $value;

    // Now save the term.
    $term
      ->save();

    // And then the entity.
    $entity
      ->save();
    $this
      ->assertEquals($term
      ->id(), $entity->field_test_taxonomy_term->entity
      ->id());
  }

  /**
   * Tests that the 'handler' field setting stores the proper plugin ID.
   */
  public function testSelectionHandlerSettings() {
    $field_name = mb_strtolower($this
      ->randomMachineName());
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'type' => 'entity_reference',
      'settings' => [
        'target_type' => 'entity_test',
      ],
    ]);
    $field_storage
      ->save();

    // Do not specify any value for the 'handler' setting in order to verify
    // that the default handler with the correct derivative is used.
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'entity_test',
    ]);
    $field
      ->save();
    $field = FieldConfig::load($field
      ->id());
    $this
      ->assertEquals('default:entity_test', $field
      ->getSetting('handler'));

    // Change the target_type in the field storage, and check that the handler
    // was correctly reassigned in the field.
    $field_storage
      ->setSetting('target_type', 'entity_test_rev');
    $field_storage
      ->save();
    $field = FieldConfig::load($field
      ->id());
    $this
      ->assertEquals('default:entity_test_rev', $field
      ->getSetting('handler'));

    // Change the handler to another, non-derivative plugin.
    $field
      ->setSetting('handler', 'views');
    $field
      ->save();
    $field = FieldConfig::load($field
      ->id());
    $this
      ->assertEquals('views', $field
      ->getSetting('handler'));

    // Change the target_type in the field storage again, and check that the
    // non-derivative handler was unchanged.
    $field_storage
      ->setSetting('target_type', 'entity_test_rev');
    $field_storage
      ->save();
    $field = FieldConfig::load($field
      ->id());
    $this
      ->assertEquals('views', $field
      ->getSetting('handler'));
  }

  /**
   * Tests ValidReferenceConstraint with newly created and unsaved entities.
   */
  public function testAutocreateValidation() {

    // The term entity is unsaved here.
    $term = Term::create([
      'name' => $this
        ->randomMachineName(),
      'vid' => $this->term
        ->bundle(),
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $entity = EntityTest::create([
      'field_test_taxonomy_term' => [
        'entity' => $term,
        'target_id' => NULL,
      ],
    ]);
    $errors = $entity
      ->validate();

    // Using target_id of NULL is valid with an unsaved entity.
    $this
      ->assertCount(0, $errors);

    // Using target_id of NULL is not valid with a saved entity.
    $term
      ->save();
    $entity = EntityTest::create([
      'field_test_taxonomy_term' => [
        'entity' => $term,
        'target_id' => NULL,
      ],
    ]);
    $errors = $entity
      ->validate();
    $this
      ->assertCount(1, $errors);
    $this
      ->assertEquals('This value should not be null.', $errors[0]
      ->getMessage());
    $this
      ->assertEquals('field_test_taxonomy_term.0', $errors[0]
      ->getPropertyPath());

    // This should rectify the issue, favoring the entity over the target_id.
    $entity
      ->save();
    $errors = $entity
      ->validate();
    $this
      ->assertCount(0, $errors);

    // Test with an unpublished and unsaved node.
    $title = $this
      ->randomString();
    $node = Node::create([
      'title' => $title,
      'type' => 'node',
      'status' => NodeInterface::NOT_PUBLISHED,
    ]);
    $entity = EntityTest::create([
      'field_test_node' => [
        'entity' => $node,
      ],
    ]);
    $errors = $entity
      ->validate();
    $this
      ->assertCount(1, $errors);
    $this
      ->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
      '%type' => 'node',
      '%label' => $title,
    ]), $errors[0]
      ->getMessage());
    $this
      ->assertEquals('field_test_node.0.entity', $errors[0]
      ->getPropertyPath());

    // Publish the node and try again.
    $node
      ->setPublished();
    $errors = $entity
      ->validate();
    $this
      ->assertCount(0, $errors);

    // Test with a mix of valid and invalid nodes.
    $unsaved_unpublished_node_title = $this
      ->randomString();
    $unsaved_unpublished_node = Node::create([
      'title' => $unsaved_unpublished_node_title,
      'type' => 'node',
      'status' => NodeInterface::NOT_PUBLISHED,
    ]);
    $saved_unpublished_node_title = $this
      ->randomString();
    $saved_unpublished_node = Node::create([
      'title' => $saved_unpublished_node_title,
      'type' => 'node',
      'status' => NodeInterface::NOT_PUBLISHED,
    ]);
    $saved_unpublished_node
      ->save();
    $saved_published_node_title = $this
      ->randomString();
    $saved_published_node = Node::create([
      'title' => $saved_published_node_title,
      'type' => 'node',
      'status' => NodeInterface::PUBLISHED,
    ]);
    $saved_published_node
      ->save();
    $entity = EntityTest::create([
      'field_test_node' => [
        [
          'entity' => $unsaved_unpublished_node,
        ],
        [
          'target_id' => $saved_unpublished_node
            ->id(),
        ],
        [
          'target_id' => $saved_published_node
            ->id(),
        ],
      ],
    ]);
    $errors = $entity
      ->validate();
    $this
      ->assertCount(2, $errors);
    $this
      ->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
      '%type' => 'node',
      '%label' => $unsaved_unpublished_node_title,
    ]), $errors[0]
      ->getMessage());
    $this
      ->assertEquals('field_test_node.0.entity', $errors[0]
      ->getPropertyPath());
    $this
      ->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
      '%type' => 'node',
      '%label' => $saved_unpublished_node
        ->id(),
    ]), $errors[1]
      ->getMessage());
    $this
      ->assertEquals('field_test_node.1.target_id', $errors[1]
      ->getPropertyPath());

    // Publish one of the nodes and try again.
    $saved_unpublished_node
      ->setPublished();
    $saved_unpublished_node
      ->save();
    $errors = $entity
      ->validate();
    $this
      ->assertCount(1, $errors);
    $this
      ->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
      '%type' => 'node',
      '%label' => $unsaved_unpublished_node_title,
    ]), $errors[0]
      ->getMessage());
    $this
      ->assertEquals('field_test_node.0.entity', $errors[0]
      ->getPropertyPath());

    // Publish the last invalid node and try again.
    $unsaved_unpublished_node
      ->setPublished();
    $errors = $entity
      ->validate();
    $this
      ->assertCount(0, $errors);

    // Test with an unpublished and unsaved comment.
    $title = $this
      ->randomString();
    $comment = Comment::create([
      'subject' => $title,
      'comment_type' => 'comment',
      'status' => 0,
    ]);
    $entity = EntityTest::create([
      'field_test_comment' => [
        'entity' => $comment,
      ],
    ]);
    $errors = $entity
      ->validate();
    $this
      ->assertCount(1, $errors);
    $this
      ->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
      '%type' => 'comment',
      '%label' => $title,
    ]), $errors[0]
      ->getMessage());
    $this
      ->assertEquals('field_test_comment.0.entity', $errors[0]
      ->getPropertyPath());

    // Publish the comment and try again.
    $comment
      ->setPublished();
    $errors = $entity
      ->validate();
    $this
      ->assertCount(0, $errors);

    // Test with an inactive and unsaved user.
    $name = $this
      ->randomString();
    $user = User::create([
      'name' => $name,
      'status' => 0,
    ]);
    $entity = EntityTest::create([
      'field_test_user' => [
        'entity' => $user,
      ],
    ]);
    $errors = $entity
      ->validate();
    $this
      ->assertCount(1, $errors);
    $this
      ->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
      '%type' => 'user',
      '%label' => $name,
    ]), $errors[0]
      ->getMessage());
    $this
      ->assertEquals('field_test_user.0.entity', $errors[0]
      ->getPropertyPath());

    // Activate the user and try again.
    $user
      ->activate();
    $errors = $entity
      ->validate();
    $this
      ->assertCount(0, $errors);

    // Test with a temporary and unsaved file.
    $filename = $this
      ->randomMachineName() . '.txt';
    $file = File::create([
      'filename' => $filename,
      'status' => 0,
    ]);
    $entity = EntityTest::create([
      'field_test_file' => [
        'entity' => $file,
      ],
    ]);
    $errors = $entity
      ->validate();
    $this
      ->assertCount(1, $errors);
    $this
      ->assertEquals(new FormattableMarkup('This entity (%type: %label) cannot be referenced.', [
      '%type' => 'file',
      '%label' => $filename,
    ]), $errors[0]
      ->getMessage());
    $this
      ->assertEquals('field_test_file.0.entity', $errors[0]
      ->getPropertyPath());

    // Set the file as permanent and try again.
    $file
      ->setPermanent();
    $errors = $entity
      ->validate();
    $this
      ->assertCount(0, $errors);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertContentTrait::$content protected property The current raw content.
AssertContentTrait::$drupalSettings protected property The drupalSettings value from the current raw $content.
AssertContentTrait::$elements protected property The XML structure parsed from the current raw $content. 1
AssertContentTrait::$plainTextContent protected property The plain-text content of raw $content (text nodes).
AssertContentTrait::assertEscaped protected function Passes if the raw text IS found escaped on the loaded page, fail otherwise.
AssertContentTrait::assertField protected function Asserts that a field exists with the given name or ID.
AssertContentTrait::assertFieldById protected function Asserts that a field exists with the given ID and value.
AssertContentTrait::assertFieldByName protected function Asserts that a field exists with the given name and value.
AssertContentTrait::assertFieldByXPath protected function Asserts that a field exists in the current page by the given XPath.
AssertContentTrait::assertFieldChecked protected function Asserts that a checkbox field in the current page is checked.
AssertContentTrait::assertFieldsByValue protected function Asserts that a field exists in the current page with a given Xpath result.
AssertContentTrait::assertLink protected function Passes if a link with the specified label is found.
AssertContentTrait::assertLinkByHref protected function Passes if a link containing a given href (part) is found.
AssertContentTrait::assertNoDuplicateIds protected function Asserts that each HTML ID is used for just a single element.
AssertContentTrait::assertNoEscaped protected function Passes if the raw text IS NOT found escaped on the loaded page, fail otherwise.
AssertContentTrait::assertNoField protected function Asserts that a field does not exist with the given name or ID.
AssertContentTrait::assertNoFieldById protected function Asserts that a field does not exist with the given ID and value.
AssertContentTrait::assertNoFieldByName protected function Asserts that a field does not exist with the given name and value.
AssertContentTrait::assertNoFieldByXPath protected function Asserts that a field does not exist or its value does not match, by XPath.
AssertContentTrait::assertNoFieldChecked protected function Asserts that a checkbox field in the current page is not checked.
AssertContentTrait::assertNoLink protected function Passes if a link with the specified label is not found.
AssertContentTrait::assertNoLinkByHref protected function Passes if a link containing a given href (part) is not found.
AssertContentTrait::assertNoLinkByHrefInMainRegion protected function Passes if a link containing a given href is not found in the main region.
AssertContentTrait::assertNoOption protected function Asserts that a select option in the current page does not exist.
AssertContentTrait::assertNoOptionSelected protected function Asserts that a select option in the current page is not checked.
AssertContentTrait::assertNoPattern protected function Triggers a pass if the perl regex pattern is not found in raw content.
AssertContentTrait::assertNoRaw protected function Passes if the raw text is NOT found on the loaded page, fail otherwise.
AssertContentTrait::assertNoText protected function Passes if the page (with HTML stripped) does not contains the text.
AssertContentTrait::assertNoTitle protected function Pass if the page title is not the given string.
AssertContentTrait::assertNoUniqueText protected function Passes if the text is found MORE THAN ONCE on the text version of the page.
AssertContentTrait::assertOption protected function Asserts that a select option in the current page exists.
AssertContentTrait::assertOptionByText protected function Asserts that a select option with the visible text exists.
AssertContentTrait::assertOptionSelected protected function Asserts that a select option in the current page is checked.
AssertContentTrait::assertOptionSelectedWithDrupalSelector protected function Asserts that a select option in the current page is checked.
AssertContentTrait::assertOptionWithDrupalSelector protected function Asserts that a select option in the current page exists.
AssertContentTrait::assertPattern protected function Triggers a pass if the Perl regex pattern is found in the raw content.
AssertContentTrait::assertRaw protected function Passes if the raw text IS found on the loaded page, fail otherwise.
AssertContentTrait::assertText protected function Passes if the page (with HTML stripped) contains the text.
AssertContentTrait::assertTextHelper protected function Helper for assertText and assertNoText.
AssertContentTrait::assertTextPattern protected function Asserts that a Perl regex pattern is found in the plain-text content.
AssertContentTrait::assertThemeOutput protected function Asserts themed output.
AssertContentTrait::assertTitle protected function Pass if the page title is the given string.
AssertContentTrait::assertUniqueText protected function Passes if the text is found ONLY ONCE on the text version of the page.
AssertContentTrait::assertUniqueTextHelper protected function Helper for assertUniqueText and assertNoUniqueText.
AssertContentTrait::buildXPathQuery protected function Builds an XPath query.
AssertContentTrait::constructFieldXpath protected function Helper: Constructs an XPath for the given set of attributes and value.
AssertContentTrait::cssSelect protected function Searches elements using a CSS selector in the raw content.
AssertContentTrait::getAllOptions protected function Get all option elements, including nested options, in a select.
AssertContentTrait::getDrupalSettings protected function Gets the value of drupalSettings for the currently-loaded page.
AssertContentTrait::getRawContent protected function Gets the current raw content.
AssertContentTrait::getSelectedItem protected function Get the selected value from a select field.
AssertContentTrait::getTextContent protected function Retrieves the plain-text content from the current raw content.
AssertContentTrait::getUrl protected function Get the current URL from the cURL handler. 1
AssertContentTrait::parse protected function Parse content returned from curlExec using DOM and SimpleXML.
AssertContentTrait::removeWhiteSpace protected function Removes all white-space between HTML tags from the raw content.
AssertContentTrait::setDrupalSettings protected function Sets the value of drupalSettings for the currently-loaded page.
AssertContentTrait::setRawContent protected function Sets the raw content (e.g. HTML).
AssertContentTrait::xpath protected function Performs an xpath search on the contents of the internal browser.
AssertLegacyTrait::assert Deprecated protected function
AssertLegacyTrait::assertEqual Deprecated protected function
AssertLegacyTrait::assertIdentical Deprecated protected function
AssertLegacyTrait::assertIdenticalObject Deprecated protected function
AssertLegacyTrait::assertNotEqual Deprecated protected function
AssertLegacyTrait::assertNotIdentical Deprecated protected function
AssertLegacyTrait::pass Deprecated protected function
AssertLegacyTrait::verbose Deprecated protected function
ConfigTestTrait::configImporter protected function Returns a ConfigImporter object to import test configuration.
ConfigTestTrait::copyConfig protected function Copies configuration objects from source storage to target storage.
EntityReferenceItemTest::$entityStringId protected property The test entity with a string ID.
EntityReferenceItemTest::$modules protected static property Modules to install. Overrides FieldKernelTestBase::$modules
EntityReferenceItemTest::$term protected property The taxonomy term to test with.
EntityReferenceItemTest::$vocabulary protected property The taxonomy vocabulary to test with.
EntityReferenceItemTest::setUp protected function Sets up the test. Overrides FieldKernelTestBase::setUp
EntityReferenceItemTest::testAutocreateValidation public function Tests ValidReferenceConstraint with newly created and unsaved entities.
EntityReferenceItemTest::testConfigEntityReferenceItem public function Tests the entity reference field type for referencing config entities.
EntityReferenceItemTest::testContentEntityReferenceItem public function Tests the entity reference field type for referencing content entities.
EntityReferenceItemTest::testContentEntityReferenceItemWithStringId public function Tests referencing content entities with string IDs.
EntityReferenceItemTest::testEntityAutoCreate public function Tests entity auto create.
EntityReferenceItemTest::testEntitySaveOrder public function Tests saving order sequence doesn't matter.
EntityReferenceItemTest::testGenerateSampleValue public function Tests the ::generateSampleValue() method.
EntityReferenceItemTest::testGenerateSampleValueCircularReference public function Tests the ::generateSampleValue() method when it has a circular reference.
EntityReferenceItemTest::testSelectionHandlerSettings public function Tests that the 'handler' field setting stores the proper plugin ID.
EntityReferenceTestTrait::createEntityReferenceField protected function Creates a field of an entity reference field storage on the specified bundle.
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
FieldKernelTestBase::$fieldTestData protected property Bag of created field storages and fields.
FieldKernelTestBase::assertFieldValues protected function Assert that a field has the expected values in an entity.
FieldKernelTestBase::createFieldWithStorage protected function Create a field and an associated field storage.
FieldKernelTestBase::entitySaveReload protected function Saves and reloads an entity.
FieldKernelTestBase::entityValidateAndSave protected function Validate and save entity. Fail if violations are found.
FieldKernelTestBase::_generateTestFieldValues protected function Generate random values for a field_test field.
KernelTestBase::$backupGlobals protected property Back up and restore any global variables that may be changed by tests.
KernelTestBase::$backupStaticAttributes protected property Back up and restore static class properties that may be changed by tests.
KernelTestBase::$backupStaticAttributesBlacklist protected property Contains a few static class properties for performance.
KernelTestBase::$classLoader protected property
KernelTestBase::$configImporter protected property @todo Move into Config test base class. 7
KernelTestBase::$configSchemaCheckerExclusions protected static property An array of config object names that are excluded from schema checking.
KernelTestBase::$container protected property
KernelTestBase::$databasePrefix protected property
KernelTestBase::$preserveGlobalState protected property Do not forward any global state from the parent process to the processes that run the actual tests.
KernelTestBase::$root protected property The app root.
KernelTestBase::$runTestInSeparateProcess protected property Kernel tests are run in separate processes because they allow autoloading of code from extensions. Running the test in a separate process isolates this behavior from other tests. Subclasses should not override this property.
KernelTestBase::$siteDirectory protected property
KernelTestBase::$strictConfigSchema protected property Set to TRUE to strict check all configuration saved. 6
KernelTestBase::$vfsRoot protected property The virtual filesystem root directory.
KernelTestBase::assertPostConditions protected function 1
KernelTestBase::bootEnvironment protected function Bootstraps a basic test environment.
KernelTestBase::bootKernel private function Bootstraps a kernel for a test.
KernelTestBase::config protected function Configuration accessor for tests. Returns non-overridden configuration.
KernelTestBase::disableModules protected function Disables modules for this test.
KernelTestBase::enableModules protected function Enables modules for this test.
KernelTestBase::getConfigSchemaExclusions protected function Gets the config schema exclusions for this test.
KernelTestBase::getDatabaseConnectionInfo protected function Returns the Database connection info to be used for this test. 3
KernelTestBase::getDatabasePrefix public function
KernelTestBase::getExtensionsForModules private function Returns Extension objects for $modules to enable.
KernelTestBase::getModulesToEnable private static function Returns the modules to enable for this test.
KernelTestBase::initFileCache protected function Initializes the FileCache component.
KernelTestBase::installConfig protected function Installs default configuration for a given list of modules.
KernelTestBase::installEntitySchema protected function Installs the storage schema for a specific entity type.
KernelTestBase::installSchema protected function Installs database tables from a module schema definition.
KernelTestBase::prepareTemplate protected function
KernelTestBase::register public function Registers test-specific services. Overrides ServiceProviderInterface::register 24
KernelTestBase::render protected function Renders a render array. 1
KernelTestBase::setInstallProfile protected function Sets the install profile and rebuilds the container to update it.
KernelTestBase::setSetting protected function Sets an in-memory Settings variable.
KernelTestBase::setUpBeforeClass public static function 1
KernelTestBase::setUpFilesystem protected function Sets up the filesystem, so things like the file directory. 2
KernelTestBase::stop protected function Stops test execution.
KernelTestBase::tearDown protected function 4
KernelTestBase::tearDownCloseDatabaseConnection public function @after
KernelTestBase::vfsDump protected function Dumps the current state of the virtual filesystem to STDOUT.
KernelTestBase::__sleep public function Prevents serializing any properties.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::$randomGenerator protected property The random generator.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers. 1
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate public function Callback for random string validation.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.
TestRequirementsTrait::checkModuleRequirements private function Checks missing module requirements.
TestRequirementsTrait::checkRequirements protected function Check module requirements for the Drupal use case. 1
TestRequirementsTrait::getDrupalRoot protected static function Returns the Drupal root directory.