/** * 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 = entity_load('entity_test', $entity->id()); $this->assertTrue($entity->field_test_taxonomy_vocabulary instanceof FieldItemListInterface, 'Field implements interface.'); $this->assertTrue($entity->field_test_taxonomy_vocabulary[0] instanceof FieldItemInterface, 'Field item implements interface.'); $this->assertEqual($entity->field_test_taxonomy_vocabulary->target_id, $referenced_entity_id); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->label(), $this->vocabulary->label()); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->id(), $referenced_entity_id); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->uuid(), $this->vocabulary->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->assertEqual($vocabulary->label(), $new_name); // Make sure the computed term reflects updates to the term id. $vocabulary2 = $vocabulary = Vocabulary::create(['name' => $this->randomMachineName(), 'vid' => Unicode::strtolower($this->randomMachineName()), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]); $vocabulary2->save(); $entity->field_test_taxonomy_vocabulary->target_id = $vocabulary2->id(); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->id(), $vocabulary2->id()); $this->assertEqual($entity->field_test_taxonomy_vocabulary->entity->label(), $vocabulary2->label()); // Delete terms so we have nothing to reference and try again $this->vocabulary->delete(); $vocabulary2->delete(); $entity = EntityTest::create(array('name' => $this->randomMachineName())); $entity->save(); }
/** * Tests term reference field and widget with multiple vocabularies. */ function testTaxonomyTermFieldMultipleVocabularies() { // Create a term in each vocabulary. $term1 = $this->createTerm($this->vocabulary1); $term2 = $this->createTerm($this->vocabulary2); // Submit an entity with both terms. $this->drupalGet('entity_test/add'); // Just check if the widget for the select is displayed, the NULL value is // used to ignore the value check. $this->assertFieldByName("{$this->fieldName}[]", NULL, 'Widget is displayed.'); $edit = array("{$this->fieldName}[]" => array($term1->id(), $term2->id())); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created.'); // Render the entity. $entity = entity_load('entity_test', $id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->setRawContent(drupal_render($content)); $this->assertText($term1->getName(), 'Term 1 name is displayed.'); $this->assertText($term2->getName(), 'Term 2 name is displayed.'); // Delete vocabulary 2. $this->vocabulary2->delete(); // Re-render the content. $entity = entity_load('entity_test', $id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->setRawContent(drupal_render($content)); // Term 1 should still be displayed; term 2 should not be. $this->assertText($term1->getName(), 'Term 1 name is displayed.'); $this->assertNoText($term2->getName(), 'Term 2 name is not displayed.'); // Verify that field storage settings and field settings are correct. $field_storage = FieldStorageConfig::loadByName('entity_test', $this->fieldName); $this->assertEqual(count($field_storage->getSetting('allowed_values')), 1, 'Only one vocabulary is allowed for the field.'); // The widget should still be displayed. $this->drupalGet('entity_test/add'); // Just check if the widget for the select is displayed, the NULL value is // used to ignore the value check. $this->assertFieldByName("{$this->fieldName}[]", NULL, 'Widget is still displayed.'); // Term 1 should still pass validation. $edit = array("{$this->fieldName}[]" => array($term1->id())); $this->drupalPostForm(NULL, $edit, t('Save')); }
/** * Test widgets. */ function testTaxonomyTermFieldWidgets() { // Create a term in the vocabulary. $term = $this->createTerm($this->vocabulary); // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName($this->fieldName, NULL, 'Widget is displayed.'); // Submit with some value. $edit = array($this->fieldName => array($term->id())); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\\d+)|', $this->url, $match); $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); // Display the object. $entity = entity_load('entity_test', $id); $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full'); $content = $display->build($entity); $this->setRawContent(drupal_render($content)); $this->assertText($term->getName(), 'Term label is displayed.'); // Delete the vocabulary and verify that the widget is gone. $this->vocabulary->delete(); $this->drupalGet('entity_test/add'); $this->assertNoFieldByName($this->fieldName, '', 'Widget is not displayed.'); }