/** * Tests using entity fields of the taxonomy term reference field type. */ public function testTaxonomyTermReferenceItem() { $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->target_id = $this->term->id(); $entity->name->value = $this->randomMachineName(); $entity->save(); $entity = entity_load('entity_test', $entity->id()); $this->assertTrue($entity->field_test_taxonomy instanceof FieldItemListInterface, 'Field implements interface.'); $this->assertTrue($entity->field_test_taxonomy[0] instanceof FieldItemInterface, 'Field item implements interface.'); $this->assertEqual($entity->field_test_taxonomy->target_id, $this->term->id(), 'Field item contains the expected TID.'); $this->assertEqual($entity->field_test_taxonomy->entity->getName(), $this->term->getName(), 'Field item entity contains the expected name.'); $this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid, 'Field item entity contains the expected ID.'); $this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid(), 'Field item entity contains the expected UUID.'); // Change the name of the term via the reference. $new_name = $this->randomMachineName(); $entity->field_test_taxonomy->entity->setName($new_name); $entity->field_test_taxonomy->entity->save(); // Verify it is the correct name. $term = Term::load($tid); $this->assertEqual($term->getName(), $new_name, 'The name of the term was changed.'); // Make sure the computed term reflects updates to the term id. $term2 = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $this->term->getVocabularyId(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $term2->save(); $entity->field_test_taxonomy->target_id = $term2->id(); $this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id(), 'Field item entity contains the new TID.'); $this->assertEqual($entity->field_test_taxonomy->entity->getName(), $term2->getName(), 'Field item entity contains the new name.'); // Test sample item generation. $entity = entity_create('entity_test'); $entity->field_test_taxonomy->generateSampleItems(); $this->entityValidateAndSave($entity); }
/** * 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(); }
/** * 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 = Term::load($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(); // Test all the possible ways of assigning a value. $entity->field_test_taxonomy_term->target_id = $term->id(); $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id()); $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term->getName()); $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()); // Test value assignment via the computed 'entity' property. $entity->field_test_taxonomy_term->entity = $term; $this->assertEqual($entity->field_test_taxonomy_term->target_id, $term->id()); $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term->getName()); $entity->field_test_taxonomy_term = [['entity' => $term2]]; $this->assertEqual($entity->field_test_taxonomy_term->target_id, $term2->id()); $this->assertEqual($entity->field_test_taxonomy_term->entity->getName(), $term2->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 (\InvalidArgumentException $e) { $this->pass('Assigning an invalid item throws an exception.'); } // 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 the generateSampleValue() method. $entity = entity_create('entity_test'); $entity->field_test_taxonomy_term->generateSampleItems(); $entity->field_test_taxonomy_vocabulary->generateSampleItems(); $this->entityValidateAndSave($entity); }
/** * Tests the views_ajax_autocomplete_taxonomy() AJAX callback. */ public function testTaxonomyAutocomplete() { $this->user = $this->drupalCreateUser(array('access content')); $this->drupalLogin($this->user); $base_autocomplete_path = 'taxonomy/autocomplete_vid/' . $this->vocabulary->vid; // Test that no terms returns an empty array. $this->assertIdentical(array(), $this->drupalGetJSON($base_autocomplete_path)); // Test a with whole name term. $label = $this->term1->getName(); $expected = array(array('value' => $label, 'label' => String::checkPlain($label))); $this->assertIdentical($expected, $this->drupalGetJSON($base_autocomplete_path, array('query' => array('q' => $label)))); // Test a term by partial name. $partial = substr($label, 0, 2); $this->assertIdentical($expected, $this->drupalGetJSON($base_autocomplete_path, array('query' => array('q' => $partial)))); }
/** * Tests output for article properties displayed in both view modes. * * @param \EasyRdf_Graph $graph * The EasyRDF graph object. * @param string $message_prefix * The word to use in the test assertion message. */ protected function assertRdfaArticleProperties($graph, $message_prefix) { // Tags. $expected_value = array('type' => 'uri', 'value' => $this->termUri); $this->assertTrue($graph->hasProperty($this->articleUri, 'http://schema.org/about', $expected_value), "{$message_prefix} tag was found (schema:about)."); // Tag type. // @todo Enable with https://www.drupal.org/node/2072791. //$this->assertEqual($graph->type($this->termUri), 'schema:Thing', 'Tag type was found (schema:Thing).'); // Tag name. $expected_value = array('type' => 'literal', 'value' => $this->term->getName(), 'lang' => 'en'); // @todo Enable with https://www.drupal.org/node/2072791. //$this->assertTrue($graph->hasProperty($this->termUri, 'http://schema.org/name', $expected_value), "$message_prefix name was found (schema:name)."); }
/** * Tests Quick Edit autocomplete term behavior. */ public function testAutocompleteQuickEdit() { $this->drupalLogin($this->editorUser); $quickedit_uri = 'quickedit/form/node/' . $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full'; $post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData(); $response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post); $ajax_commands = Json::decode($response); // Prepare form values for submission. drupalPostAJAX() is not suitable for // handling pages with JSON responses, so we need our own solution here. $form_tokens_found = preg_match('/\\sname="form_token" value="([^"]+)"/', $ajax_commands[0]['data'], $token_match) && preg_match('/\\sname="form_build_id" value="([^"]+)"/', $ajax_commands[0]['data'], $build_id_match); $this->assertTrue($form_tokens_found, 'Form tokens found in output.'); if ($form_tokens_found) { $post = array('form_id' => 'quickedit_field_form', 'form_token' => $token_match[1], 'form_build_id' => $build_id_match[1], $this->fieldName . '[target_id]' => implode(', ', array($this->term1->getName(), 'new term', $this->term2->getName())), 'op' => t('Save')); // Submit field form and check response. Should render back all the terms. $response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post); $this->assertResponse(200); $ajax_commands = Json::decode($response); $this->setRawContent($ajax_commands[0]['data']); $this->assertLink($this->term1->getName()); $this->assertLink($this->term2->getName()); $this->assertText('new term'); $this->assertNoLink('new term'); // Load the form again, which should now get it back from // PrivateTempStore. $quickedit_uri = 'quickedit/form/node/' . $this->node->id() . '/' . $this->fieldName . '/' . $this->node->language()->getId() . '/full'; $post = array('nocssjs' => 'true') + $this->getAjaxPageStatePostData(); $response = $this->drupalPost($quickedit_uri, 'application/vnd.drupal-ajax', $post); $ajax_commands = Json::decode($response); // The AjaxResponse's first command is an InsertCommand which contains // the form to edit the taxonomy term field, it should contain all three // taxonomy terms, including the one that has just been newly created and // which is not yet stored. $this->setRawContent($ajax_commands[0]['data']); $expected = array($this->term1->getName() . ' (' . $this->term1->id() . ')', 'new term', $this->term2->getName() . ' (' . $this->term2->id() . ')'); $this->assertFieldByName($this->fieldName . '[target_id]', implode(', ', $expected)); // Save the entity. $post = array('nocssjs' => 'true'); $response = $this->drupalPostWithFormat('quickedit/entity/node/' . $this->node->id(), 'json', $post); $this->assertResponse(200); // The full node display should now link to all entities, with the new // one created in the database as well. $this->drupalGet('node/' . $this->node->id()); $this->assertLink($this->term1->getName()); $this->assertLink($this->term2->getName()); $this->assertLink('new term'); } }
/** * Route title callback. * * @param \Drupal\taxonomy\TermInterface $taxonomy_term * The taxonomy term. * * @return string * The term label. */ public function termTitle(TermInterface $taxonomy_term) { return Xss::filter($taxonomy_term->getName()); }
/** * Route title callback. * * @param \Drupal\taxonomy\TermInterface $taxonomy_term * The taxonomy term. * * @return array * The term label as a render array. */ public function termTitle(TermInterface $taxonomy_term) { return ['#markup' => $taxonomy_term->getName(), '#allowed_tags' => Xss::getHtmlTagList()]; }
/** * Returns a renderable forum index page array. * * @param array $forums * A list of forums. * @param \Drupal\taxonomy\TermInterface $term * The taxonomy term of the forum. * @param array $topics * The topics of this forum. * @param array $parents * The parent forums in relation this forum. * @param array $header * Array of header cells. * * @return array * A render array. */ protected function build($forums, TermInterface $term, $topics = array(), $parents = array(), $header = array()) { $config = $this->config('forum.settings'); $build = array('#theme' => 'forums', '#forums' => $forums, '#topics' => $topics, '#parents' => $parents, '#header' => $header, '#term' => $term, '#sortby' => $config->get('topics.order'), '#forums_per_page' => $config->get('topics.page_limit')); $build['#attached']['library'][] = 'forum/forum.index'; if (empty($term->forum_container->value)) { $build['#attached']['drupal_add_feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName()); } return $build; }
/** * Route title callback. * * @param \Drupal\taxonomy\TermInterface $taxonomy_term * The taxonomy term. * * @return string * The term label. */ public function termTitle(TermInterface $taxonomy_term) { return SafeMarkup::xssFilter($taxonomy_term->getName()); }
/** * Returns a renderable forum index page array. * * @param array $forums * A list of forums. * @param \Drupal\taxonomy\TermInterface $term * The taxonomy term of the forum. * @param array $topics * The topics of this forum. * @param array $parents * The parent forums in relation this forum. * @param array $header * Array of header cells. * * @return array * A render array. */ protected function build($forums, TermInterface $term, $topics = array(), $parents = array(), $header = array()) { $config = $this->config('forum.settings'); $build = array('#theme' => 'forums', '#forums' => $forums, '#topics' => $topics, '#parents' => $parents, '#header' => $header, '#term' => $term, '#sortby' => $config->get('topics.order'), '#forums_per_page' => $config->get('topics.page_limit')); $build['#attached']['library'][] = 'forum/forum.index'; if (empty($term->forum_container->value)) { $build['#attached']['feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName()); } $this->renderer->addCacheableDependency($build, $config); return ['action' => $this->buildActionLinks($config->get('vocabulary'), $term), 'forum' => $build]; }
/** * Returns a renderable forum index page array. * * @param array $forums * A list of forums. * @param \Drupal\taxonomy\TermInterface $term * The taxonomy term of the forum. * @param array $topics * The topics of this forum. * @param array $parents * The parent forums in relation this forum. * @param array $header * Array of header cells. * * @return array * A render array. */ protected function build($forums, TermInterface $term, $topics = array(), $parents = array(), $header = array()) { $config = $this->config('forum.settings'); $build = array('#theme' => 'forums', '#forums' => $forums, '#topics' => $topics, '#parents' => $parents, '#header' => $header, '#term' => $term, '#sortby' => $config->get('topics.order'), '#forums_per_page' => $config->get('topics.page_limit')); if (empty($term->forum_container->value)) { $build['#attached']['feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName()); } $this->renderer->addCacheableDependency($build, $config); foreach ($forums as $forum) { $this->renderer->addCacheableDependency($build, $forum); } foreach ($topics as $topic) { $this->renderer->addCacheableDependency($build, $topic); } foreach ($parents as $parent) { $this->renderer->addCacheableDependency($build, $parent); } $this->renderer->addCacheableDependency($build, $term); return ['action' => $this->buildActionLinks($config->get('vocabulary'), $term), 'forum' => $build, '#cache' => ['tags' => Cache::mergeTags($this->nodeEntityTypeDefinition->getListCacheTags(), $this->commentEntityTypeDefinition->getListCacheTags())]]; }