Commit 7a83b8f3 authored by webchick's avatar webchick

Issue #1594218 by aspilicious, Rob Loach: Convert taxonomy tests to PSR-0.

parent 499c1cdd
......@@ -8,6 +8,7 @@
use Drupal\node\Node;
use Drupal\simpletest\WebTestBase;
use Drupal\comment\Tests\CommentTestBase;
use Drupal\taxonomy\Tests\TaxonomyTestBase;
class RdfMappingHookTestCase extends WebTestBase {
public static function getInfo() {
......@@ -282,7 +283,7 @@ class RdfCrudTestCase extends WebTestBase {
}
}
class RdfMappingDefinitionTestCase extends TaxonomyWebTestCase {
class RdfMappingDefinitionTestCase extends TaxonomyTestBase {
protected $profile = 'standard';
public static function getInfo() {
......
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\EfqTest.
*/
namespace Drupal\taxonomy\Tests;
use Drupal\entity\EntityFieldQuery;
/**
* Tests the functionality of EntityFieldQuery for taxonomy entities.
*/
class EfqTest extends TaxonomyTestBase {
public static function getInfo() {
return array(
'name' => 'Taxonomy EntityFieldQuery',
'description' => 'Verifies operation of a taxonomy-based EntityFieldQuery.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
$this->drupalLogin($this->admin_user);
$this->vocabulary = $this->createVocabulary();
}
/**
* Tests that a basic taxonomy EntityFieldQuery works.
*/
function testTaxonomyEfq() {
$terms = array();
for ($i = 0; $i < 5; $i++) {
$term = $this->createTerm($this->vocabulary);
$terms[$term->tid] = $term;
}
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'taxonomy_term');
$result = $query->execute();
$result = $result['taxonomy_term'];
asort($result);
$this->assertEqual(array_keys($terms), array_keys($result), 'Taxonomy terms were retrieved by EntityFieldQuery.');
// Create a second vocabulary and five more terms.
$vocabulary2 = $this->createVocabulary();
$terms2 = array();
for ($i = 0; $i < 5; $i++) {
$term = $this->createTerm($vocabulary2);
$terms2[$term->tid] = $term;
}
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'taxonomy_term');
$query->entityCondition('bundle', $vocabulary2->machine_name);
$result = $query->execute();
$result = $result['taxonomy_term'];
asort($result);
$this->assertEqual(array_keys($terms2), array_keys($result), format_string('Taxonomy terms from the %name vocabulary were retrieved by EntityFieldQuery.', array('%name' => $vocabulary2->name)));
}
}
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\HooksTest.
*/
namespace Drupal\taxonomy\Tests;
/**
* Tests for taxonomy hook invocation.
*/
class HooksTest extends TaxonomyTestBase {
public static function getInfo() {
return array(
'name' => 'Taxonomy term hooks',
'description' => 'Hooks for taxonomy term load/save/delete.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp(array('taxonomy_test'));
$taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
$this->drupalLogin($taxonomy_admin);
}
/**
* Test that hooks are run correctly on creating, editing and deleting a term.
*/
function testTaxonomyTermHooks() {
$vocabulary = $this->createVocabulary();
// Create a term with one antonym.
$edit = array(
'name' => $this->randomName(),
'antonym' => 'Long',
);
$this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save'));
$terms = taxonomy_term_load_multiple_by_name($edit['name']);
$term = reset($terms);
$this->assertEqual($term->antonym, $edit['antonym'], 'Antonym was loaded into the term object.');
// Update the term with a different antonym.
$edit = array(
'name' => $this->randomName(),
'antonym' => 'Short',
);
$this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
taxonomy_terms_static_reset();
$term = taxonomy_term_load($term->tid);
$this->assertEqual($edit['antonym'], $term->antonym, 'Antonym was successfully edited.');
// Delete the term.
taxonomy_term_delete($term->tid);
$antonym = db_query('SELECT tid FROM {taxonomy_term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
$this->assertFalse($antonym, 'The antonym were deleted from the database.');
}
}
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\LegacyTest.
*/
namespace Drupal\taxonomy\Tests;
/**
* Test for legacy node bug.
*/
class LegacyTest extends TaxonomyTestBase {
protected $profile = 'standard';
public static function getInfo() {
return array(
'name' => 'Test for legacy node bug.',
'description' => 'Posts an article with a taxonomy term and a date prior to 1970.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access'));
$this->drupalLogin($this->admin_user);
}
/**
* Test taxonomy functionality with nodes prior to 1970.
*/
function testTaxonomyLegacyNode() {
// Posts an article with a taxonomy term and a date prior to 1970.
$langcode = LANGUAGE_NOT_SPECIFIED;
$edit = array();
$edit['title'] = $this->randomName();
$edit['date'] = '1969-01-01 00:00:00 -0500';
$edit["body[$langcode][0][value]"] = $this->randomName();
$edit["field_tags[$langcode]"] = $this->randomName();
$this->drupalPost('node/add/article', $edit, t('Save'));
// Checks that the node has been saved.
$node = $this->drupalGetNodeByTitle($edit['title']);
$this->assertEqual($node->created, strtotime($edit['date']), 'Legacy node was saved with the right date.');
}
}
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\LoadMultipleTest.
*/
namespace Drupal\taxonomy\Tests;
/**
* Test the taxonomy_term_load_multiple() function.
*/
class LoadMultipleTest extends TaxonomyTestBase {
public static function getInfo() {
return array(
'name' => 'Taxonomy term multiple loading',
'description' => 'Test the loading of multiple taxonomy terms at once',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp();
$this->taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
$this->drupalLogin($this->taxonomy_admin);
}
/**
* Create a vocabulary and some taxonomy terms, ensuring they're loaded
* correctly using taxonomy_term_load_multiple().
*/
function testTaxonomyTermMultipleLoad() {
// Create a vocabulary.
$vocabulary = $this->createVocabulary();
// Create five terms in the vocabulary.
$i = 0;
while ($i < 5) {
$i++;
$this->createTerm($vocabulary);
}
// Load the terms from the vocabulary.
$terms = taxonomy_term_load_multiple(FALSE, array('vid' => $vocabulary->vid));
$count = count($terms);
$this->assertEqual($count, 5, format_string('Correct number of terms were loaded. !count terms.', array('!count' => $count)));
// Load the same terms again by tid.
$terms2 = taxonomy_term_load_multiple(array_keys($terms));
$this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.');
$this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.');
// Load the terms by tid, with a condition on vid.
$terms3 = taxonomy_term_load_multiple(array_keys($terms2), array('vid' => $vocabulary->vid));
$this->assertEqual($terms2, $terms3, 'Same terms found when limiting load to vocabulary.');
// Remove one term from the array, then delete it.
$deleted = array_shift($terms3);
taxonomy_term_delete($deleted->tid);
$deleted_term = taxonomy_term_load($deleted->tid);
$this->assertFalse($deleted_term);
// Load terms from the vocabulary by vid.
$terms4 = taxonomy_term_load_multiple(FALSE, array('vid' => $vocabulary->vid));
$this->assertEqual(count($terms4), 4, 'Correct number of terms were loaded.');
$this->assertFalse(isset($terms4[$deleted->tid]));
// Create a single term and load it by name.
$term = $this->createTerm($vocabulary);
$loaded_terms = taxonomy_term_load_multiple(array(), array('name' => $term->name));
$this->assertEqual(count($loaded_terms), 1, 'One term was loaded.');
$loaded_term = reset($loaded_terms);
$this->assertEqual($term->tid, $loaded_term->tid, 'Term loaded by name successfully.');
}
}
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\RssTest.
*/
namespace Drupal\taxonomy\Tests;
/**
* Tests the rendering of term reference fields in RSS feeds.
*/
class RssTest extends TaxonomyTestBase {
public static function getInfo() {
return array(
'name' => 'Taxonomy RSS Content.',
'description' => 'Ensure that data added as terms appears in RSS feeds if "RSS Category" format is selected.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp(array('node', 'field_ui'));
$this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access', 'administer content types'));
$this->drupalLogin($this->admin_user);
$this->vocabulary = $this->createVocabulary();
$field = array(
'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $this->vocabulary->machine_name,
'parent' => 0,
),
),
),
);
field_create_field($field);
$this->instance = array(
'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
'bundle' => 'article',
'entity_type' => 'node',
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
),
),
);
field_create_instance($this->instance);
}
/**
* Tests that terms added to nodes are displayed in core RSS feed.
*
* Create a node and assert that taxonomy terms appear in rss.xml.
*/
function testTaxonomyRss() {
// Create two taxonomy terms.
$term1 = $this->createTerm($this->vocabulary);
// RSS display must be added manually.
$this->drupalGet("admin/structure/types/manage/article/display");
$edit = array(
"view_modes_custom[rss]" => '1',
);
$this->drupalPost(NULL, $edit, t('Save'));
// Change the format to 'RSS category'.
$this->drupalGet("admin/structure/types/manage/article/display/rss");
$edit = array(
"fields[taxonomy_" . $this->vocabulary->machine_name . "][type]" => 'taxonomy_term_reference_rss_category',
);
$this->drupalPost(NULL, $edit, t('Save'));
// Post an article.
$edit = array();
$langcode = LANGUAGE_NOT_SPECIFIED;
$edit["title"] = $this->randomName();
$edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid;
$this->drupalPost('node/add/article', $edit, t('Save'));
// Check that the term is displayed when the RSS feed is viewed.
$this->drupalGet('rss.xml');
$test_element = array(
'key' => 'category',
'value' => $term1->name,
'attributes' => array(
'domain' => url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE)),
),
);
$this->assertRaw(format_xml_elements(array($test_element)), 'Term is displayed when viewing the rss feed.');
}
}
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\TaxonomyTestBase.
*/
namespace Drupal\taxonomy\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Provides common helper methods for Taxonomy module tests.
*/
class TaxonomyTestBase extends WebTestBase {
function setUp() {
$modules = func_get_args();
if (isset($modules[0]) && is_array($modules[0])) {
$modules = $modules[0];
}
$modules[] = 'taxonomy';
parent::setUp($modules);
// Create Basic page and Article node types.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
}
}
/**
* Returns a new vocabulary with random properties.
*/
function createVocabulary() {
// Create a vocabulary.
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomName(),
'description' => $this->randomName(),
'machine_name' => drupal_strtolower($this->randomName()),
'langcode' => LANGUAGE_NOT_SPECIFIED,
'help' => '',
'nodes' => array('article' => 'article'),
'weight' => mt_rand(0, 10),
));
taxonomy_vocabulary_save($vocabulary);
return $vocabulary;
}
/**
* Returns a new term with random properties in vocabulary $vid.
*/
function createTerm($vocabulary) {
$term = entity_create('taxonomy_term', array(
'name' => $this->randomName(),
'description' => $this->randomName(),
// Use the first available text format.
'format' => db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField(),
'vid' => $vocabulary->vid,
'langcode' => LANGUAGE_NOT_SPECIFIED,
));
taxonomy_term_save($term);
return $term;
}
}
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\TermFieldMultipleVocabularyTest.
*/
namespace Drupal\taxonomy\Tests;
/**
* Tests a taxonomy term reference field that allows multiple vocabularies.
*/
class TermFieldMultipleVocabularyTest extends TaxonomyTestBase {
protected $instance;
protected $vocabulary1;
protected $vocabulary2;
public static function getInfo() {
return array(
'name' => 'Multiple vocabulary term reference field',
'description' => 'Tests term reference fields that allow multiple vocabularies.',
'group' => 'Taxonomy',
);
}
function setUp() {
parent::setUp('field_test');
$web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy'));
$this->drupalLogin($web_user);
$this->vocabulary1 = $this->createVocabulary();
$this->vocabulary2 = $this->createVocabulary();
// Set up a field and instance.
$this->field_name = drupal_strtolower($this->randomName());
$this->field = array(
'field_name' => $this->field_name,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $this->vocabulary1->machine_name,
'parent' => '0',
),
array(
'vocabulary' => $this->vocabulary2->machine_name,
'parent' => '0',
),
),
)
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field_name,
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'full' => array(
'type' => 'taxonomy_term_reference_link',
),
),
);
field_create_instance($this->instance);
}
/**
* 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.
$langcode = LANGUAGE_NOT_SPECIFIED;
$this->drupalGet('test-entity/add/test_bundle');
$this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is displayed');
$edit = array(
"{$this->field_name}[$langcode][]" => array($term1->tid, $term2->tid),
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.');
// Render the entity.
$entity = field_test_entity_test_load($id);
$entities = array($id => $entity);
field_attach_prepare_view('test_entity', $entities, 'full');
$entity->content = field_attach_view('test_entity', $entity, 'full');
$this->content = drupal_render($entity->content);
$this->assertText($term1->name, 'Term 1 name is displayed.');
$this->assertText($term2->name, 'Term 2 name is displayed.');
// Delete vocabulary 2.
taxonomy_vocabulary_delete($this->vocabulary2->vid);
// Re-render the content.
$entity = field_test_entity_test_load($id);
$entities = array($id => $entity);
field_attach_prepare_view('test_entity', $entities, 'full');
$entity->content = field_attach_view('test_entity', $entity, 'full');
$this->plainTextContent = FALSE;
$this->content = drupal_render($entity->content);
// Term 1 should still be displayed; term 2 should not be.
$this->assertText($term1->name, 'Term 1 name is displayed.');
$this->assertNoText($term2->name, 'Term 2 name is not displayed.');
// Verify that field and instance settings are correct.
$field_info = field_info_field($this->field_name);
$this->assertEqual(sizeof($field_info['settings']['allowed_values']), 1, 'Only one vocabulary is allowed for the field.');
// The widget should still be displayed.
$this->drupalGet('test-entity/add/test_bundle');
$this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is still displayed');
// Term 1 should still pass validation.
$edit = array(
"{$this->field_name}[$langcode][]" => array($term1->tid),
);
$this->drupalPost(NULL, $edit, t('Save'));
}
}
<?php
/**
* @file
* Definition of Drupal\taxonomy\Tests\TermFieldTest.
*/
namespace Drupal\taxonomy\Tests;
use Drupal\field\FieldValidationException;
/**
* Tests for taxonomy term field and formatter.
*/
class TermFieldTest extends TaxonomyTestBase {
protected $instance;
protected $vocabulary;