/**
  * 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 = entity_create('entity_test');
     $entity->field_test_taxonomy_term->target_id = $tid;
     $entity->name->value = $this->randomMachineName();
     $entity->save();
     $entity = entity_load('entity_test', $entity->id());
     $this->assertTrue($entity->field_test_taxonomy_term instanceof FieldItemListInterface, 'Field implements interface.');
     $this->assertTrue($entity->field_test_taxonomy_term[0] instanceof FieldItemInterface, 'Field item implements interface.');
     $this->assertEqual($entity->field_test_taxonomy_term->target_id, $tid);
     $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $this->term->getName());
     $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $tid);
     $this->assertEqual($entity->field_test_taxonomy_term->entity->uuid(), $this->term->uuid());
     // 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 = entity_load('taxonomy_term', $tid);
     $this->assertEqual($term->getName(), $new_name);
     // Make sure the computed term reflects updates to the term id.
     $term2 = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $this->term->bundle(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $term2->save();
     $entity->field_test_taxonomy_term->target_id = $term2->id();
     $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term2->id());
     $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term2->getName());
     // Delete terms so we have nothing to reference and try again
     $term->delete();
     $term2->delete();
     $entity = entity_create('entity_test', array('name' => $this->randomMachineName()));
     $entity->save();
 }
 /**
  * Test saving order sequence doesn't matter.
  */
 public function testEntitySaveOrder()
 {
     // The term entity is unsaved here.
     $term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $this->term->bundle(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED));
     $entity = entity_create('entity_test');
     // Now assign the unsaved term to the field.
     $entity->field_test_taxonomy_term->entity = $term;
     $entity->name->value = $this->randomMachineName();
     // Now save the term.
     $term->save();
     // And then the entity.
     $entity->save();
     $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id());
 }
 /**
  * Tests validation constraint.
  */
 public function testValidation()
 {
     // The term entity is unsaved here.
     $term = Term::create(array('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->assertEqual(0, count($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->assertEqual(1, count($errors));
     $this->assertEqual($errors[0]->getMessage(), 'This value should not be null.');
     $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_taxonomy_term.0');
     // This should rectify the issue, favoring the entity over the target_id.
     $entity->save();
     $errors = $entity->validate();
     $this->assertEqual(0, count($errors));
 }
 /**
  * Tests ValidReferenceConstraint with newly created and unsaved entities.
  */
 public function testAutocreateValidation()
 {
     // The term entity is unsaved here.
     $term = Term::create(array('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->assertEqual(0, count($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->assertEqual(1, count($errors));
     $this->assertEqual($errors[0]->getMessage(), 'This value should not be null.');
     $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_taxonomy_term.0');
     // This should rectify the issue, favoring the entity over the target_id.
     $entity->save();
     $errors = $entity->validate();
     $this->assertEqual(0, count($errors));
     // Test with an unpublished and unsaved node.
     $title = $this->randomString();
     $node = Node::create(['title' => $title, 'type' => 'node', 'status' => NODE_NOT_PUBLISHED]);
     $entity = EntityTest::create(['field_test_node' => ['entity' => $node]]);
     $errors = $entity->validate();
     $this->assertEqual(1, count($errors));
     $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $title]));
     $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
     // Publish the node and try again.
     $node->setPublished(TRUE);
     $errors = $entity->validate();
     $this->assertEqual(0, count($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' => NODE_NOT_PUBLISHED]);
     $saved_unpublished_node_title = $this->randomString();
     $saved_unpublished_node = Node::create(['title' => $saved_unpublished_node_title, 'type' => 'node', 'status' => NODE_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' => NODE_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->assertEqual(2, count($errors));
     $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title]));
     $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
     $this->assertEqual($errors[1]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $saved_unpublished_node->id()]));
     $this->assertEqual($errors[1]->getPropertyPath(), 'field_test_node.1.target_id');
     // Publish one of the nodes and try again.
     $saved_unpublished_node->setPublished(TRUE);
     $saved_unpublished_node->save();
     $errors = $entity->validate();
     $this->assertEqual(1, count($errors));
     $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title]));
     $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity');
     // Publish the last invalid node and try again.
     $unsaved_unpublished_node->setPublished(TRUE);
     $errors = $entity->validate();
     $this->assertEqual(0, count($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->assertEqual(1, count($errors));
     $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'comment', '%label' => $title]));
     $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_comment.0.entity');
     // Publish the comment and try again.
     $comment->setPublished(TRUE);
     $errors = $entity->validate();
     $this->assertEqual(0, count($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->assertEqual(1, count($errors));
     $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'user', '%label' => $name]));
     $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_user.0.entity');
     // Activate the user and try again.
     $user->activate();
     $errors = $entity->validate();
     $this->assertEqual(0, count($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->assertEqual(1, count($errors));
     $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'file', '%label' => $filename]));
     $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_file.0.entity');
     // Set the file as permanent and try again.
     $file->setPermanent();
     $errors = $entity->validate();
     $this->assertEqual(0, count($errors));
 }
Exemple #5
0
 /**
  * Generates an action link to display at the top of the forum listing.
  *
  * @param string $vid
  *   Vocabulary ID.
  * @param \Drupal\taxonomy\TermInterface $forum_term
  *   The term for which the links are to be built.
  *
  * @return array
  *   Render array containing the links.
  */
 protected function buildActionLinks($vid, TermInterface $forum_term = NULL)
 {
     $user = $this->currentUser();
     $links = [];
     // Loop through all bundles for forum taxonomy vocabulary field.
     foreach ($this->fieldMap['node']['taxonomy_forums']['bundles'] as $type) {
         if ($this->nodeAccess->createAccess($type)) {
             $links[$type] = ['#attributes' => ['class' => ['action-links']], '#theme' => 'menu_local_action', '#link' => ['title' => $this->t('Add new @node_type', ['@node_type' => $this->nodeTypeStorage->load($type)->label()]), 'url' => Url::fromRoute('node.add', ['node_type' => $type])]];
             if ($forum_term && $forum_term->bundle() == $vid) {
                 // We are viewing a forum term (specific forum), append the tid to
                 // the url.
                 $links[$type]['#link']['localized_options']['query']['forum_id'] = $forum_term->id();
             }
         }
     }
     if (empty($links)) {
         // Authenticated user does not have access to create new topics.
         if ($user->isAuthenticated()) {
             $links['disallowed'] = ['#markup' => $this->t('You are not allowed to post new content in the forum.')];
         } else {
             $links['login'] = ['#attributes' => ['class' => ['action-links']], '#theme' => 'menu_local_action', '#link' => array('title' => $this->t('Log in to post new content in the forum.'), 'url' => Url::fromRoute('user.login', [], ['query' => $this->getDestinationArray()]))];
         }
     }
     return $links;
 }