12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985 |
- <?php
- class TaxonomyWebTestCase extends DrupalWebTestCase {
-
- function createVocabulary() {
-
- $vocabulary = new stdClass();
- $vocabulary->name = $this->randomName();
- $vocabulary->description = $this->randomName();
- $vocabulary->machine_name = drupal_strtolower($this->randomName());
- $vocabulary->help = '';
- $vocabulary->nodes = array('article' => 'article');
- $vocabulary->weight = mt_rand(0, 10);
- taxonomy_vocabulary_save($vocabulary);
- return $vocabulary;
- }
-
- function createTerm($vocabulary) {
- $term = new stdClass();
- $term->name = $this->randomName();
- $term->description = $this->randomName();
-
- $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
- $term->vid = $vocabulary->vid;
- taxonomy_term_save($term);
- return $term;
- }
- }
- class TaxonomyVocabularyFunctionalTest extends TaxonomyWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy vocabulary interface',
- 'description' => 'Test the taxonomy vocabulary interface.',
- 'group' => 'Taxonomy',
- );
- }
- function setUp() {
- parent::setUp();
- $this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
- $this->drupalLogin($this->admin_user);
- $this->vocabulary = $this->createVocabulary();
- }
-
- function testVocabularyInterface() {
-
- $this->drupalGet('admin/structure/taxonomy');
-
- $this->clickLink(t('Add vocabulary'));
- $edit = array();
- $machine_name = drupal_strtolower($this->randomName());
- $edit['name'] = $this->randomName();
- $edit['description'] = $this->randomName();
- $edit['machine_name'] = $machine_name;
- $this->drupalPost(NULL, $edit, t('Save'));
- $this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), 'Vocabulary created successfully.');
-
- $this->drupalGet('admin/structure/taxonomy');
- $this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.');
- $this->clickLink(t('edit vocabulary'));
- $edit = array();
- $edit['name'] = $this->randomName();
- $this->drupalPost(NULL, $edit, t('Save'));
- $this->drupalGet('admin/structure/taxonomy');
- $this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.');
-
- $edit['machine_name'] = $machine_name;
- $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
- $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
-
- $edit['machine_name'] = '!&^%';
- $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
- $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
-
- $edit = array();
- $edit['name'] = 'Don\'t Panic';
- $edit['description'] = $this->randomName();
- $edit['machine_name'] = 'don_t_panic';
- $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
- $site_name = variable_get('site_name', 'Drupal');
- $this->drupalGet('admin/structure/taxonomy/don_t_panic');
- $this->assertTitle(t('Don\'t Panic | @site-name', array('@site-name' => $site_name)));
- $this->assertNoTitle(t('Don't Panic | @site-name', array('@site-name' => $site_name)));
- }
-
- function testTaxonomyAdminChangingWeights() {
-
- for ($i = 0; $i < 10; $i++) {
- $this->createVocabulary();
- }
-
- $vocabularies = taxonomy_get_vocabularies();
- $edit = array();
- foreach ($vocabularies as $key => $vocabulary) {
- $vocabulary->weight = -$vocabulary->weight;
- $vocabularies[$key]->weight = $vocabulary->weight;
- $edit[$key . '[weight]'] = $vocabulary->weight;
- }
-
- $this->drupalPost('admin/structure/taxonomy', $edit, t('Save'));
-
- $new_vocabularies = taxonomy_get_vocabularies();
-
- foreach ($vocabularies as $key => $vocabulary) {
- $this->assertEqual($new_vocabularies[$key]->weight, $vocabularies[$key]->weight, 'The vocabulary weight was changed.');
- }
- }
-
- function testTaxonomyAdminNoVocabularies() {
-
- $vocabularies = taxonomy_get_vocabularies();
- foreach ($vocabularies as $key => $vocabulary) {
- taxonomy_vocabulary_delete($key);
- }
-
- $this->assertFalse(taxonomy_get_vocabularies(), 'No vocabularies found in the database.');
- $this->drupalGet('admin/structure/taxonomy');
-
- $this->assertText(t('No vocabularies available.'), 'No vocabularies were found.');
- }
-
- function testTaxonomyAdminDeletingVocabulary() {
-
- $edit = array(
- 'name' => $this->randomName(),
- 'machine_name' => drupal_strtolower($this->randomName()),
- );
- $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
- $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
-
- $vocabularies = taxonomy_get_vocabularies();
- $vocabularies_keys = array_keys($vocabularies);
- $vid = $vocabularies[end($vocabularies_keys)]->vid;
- entity_get_controller('taxonomy_vocabulary')->resetCache();
- $vocabulary = taxonomy_vocabulary_load($vid);
- $this->assertTrue($vocabulary, 'Vocabulary found in database.');
-
- $edit = array();
- $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/edit', $edit, t('Delete'));
- $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', array('%name' => $vocabulary->name)), '[confirm deletion] Asks for confirmation.');
- $this->assertText(t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'), '[confirm deletion] Inform that all terms will be deleted.');
-
- $this->drupalPost(NULL, NULL, t('Delete'));
- $this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), 'Vocabulary deleted.');
- entity_get_controller('taxonomy_vocabulary')->resetCache();
- $this->assertFalse(taxonomy_vocabulary_load($vid), 'Vocabulary is not found in the database.');
- }
- }
- class TaxonomyVocabularyTestCase extends TaxonomyWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy vocabularies',
- 'description' => 'Test loading, saving and deleting vocabularies.',
- 'group' => 'Taxonomy',
- );
- }
- function setUp() {
- parent::setUp('taxonomy', 'field_test');
- $admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy'));
- $this->drupalLogin($admin_user);
- $this->vocabulary = $this->createVocabulary();
- }
-
- function testTaxonomyVocabularyLoadReturnFalse() {
-
- $vocabularies = taxonomy_get_vocabularies();
- $vid = count($vocabularies) + 1;
- $vocabulary = taxonomy_vocabulary_load($vid);
-
- $this->assertTrue(empty($vocabulary), 'No object loaded.');
-
- $this->createVocabulary();
-
-
- $vocabulary = taxonomy_vocabulary_load($vid);
- $this->assertTrue(!empty($vocabulary) && is_object($vocabulary), 'Vocabulary is an object.');
- $this->assertEqual($vocabulary->vid, $vid, 'Valid vocabulary vid is the same as our previously invalid one.');
- }
-
- function testTaxonomyVocabularyDeleteWithTerms() {
-
- foreach (taxonomy_get_vocabularies() as $vocabulary) {
- taxonomy_vocabulary_delete($vocabulary->vid);
- }
-
- $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
-
- $vocabulary = $this->createVocabulary();
- $terms = array();
- for ($i = 0; $i < 5; $i++) {
- $terms[$i] = $this->createTerm($vocabulary);
- }
-
- $terms[2]->parent = array($terms[1]->tid);
- taxonomy_term_save($terms[2]);
- $terms[4]->parent = array($terms[1]->tid, $terms[2]->tid);
- taxonomy_term_save($terms[4]);
-
- $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
- taxonomy_vocabulary_delete($vocabulary->vid);
-
- $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
- }
-
- function testTaxonomyVocabularyLoadStaticReset() {
- $original_vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
- $this->assertTrue(is_object($original_vocabulary), 'Vocabulary loaded successfully.');
- $this->assertEqual($this->vocabulary->name, $original_vocabulary->name, 'Vocabulary loaded successfully.');
-
- $vocabulary = $original_vocabulary;
- $vocabulary->name = $this->randomName();
- $vocabulary->description = $this->randomName();
- taxonomy_vocabulary_save($vocabulary);
-
- $new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid);
- $this->assertEqual($new_vocabulary->name, $vocabulary->name);
- $this->assertEqual($new_vocabulary->name, $vocabulary->name);
-
- taxonomy_vocabulary_delete($this->vocabulary->vid);
- $vocabularies = taxonomy_get_vocabularies();
- $this->assertTrue(!isset($vocabularies[$this->vocabulary->vid]), 'The vocabulary was deleted.');
- }
-
- function testTaxonomyVocabularyLoadMultiple() {
-
- foreach (taxonomy_get_vocabularies() as $vocabulary) {
- taxonomy_vocabulary_delete($vocabulary->vid);
- }
-
- $vocabulary1 = $this->createVocabulary();
- $vocabulary1->weight = 0;
- taxonomy_vocabulary_save($vocabulary1);
- $vocabulary2 = $this->createVocabulary();
- $vocabulary2->weight = 1;
- taxonomy_vocabulary_save($vocabulary2);
- $vocabulary3 = $this->createVocabulary();
- $vocabulary3->weight = 2;
- taxonomy_vocabulary_save($vocabulary3);
-
-
- $names = taxonomy_vocabulary_get_names();
- $this->assertEqual($names[$vocabulary1->machine_name]->name, $vocabulary1->name, 'Vocabulary 1 name found.');
-
-
- $vocabularies = taxonomy_get_vocabularies();
- $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, 'Vocabulary was found in the vocabularies array.');
- $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, 'Vocabulary was found in the vocabularies array.');
- $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, 'Vocabulary was found in the vocabularies array.');
-
-
- $vocabularies = taxonomy_vocabulary_load_multiple(array($vocabulary3->vid, $vocabulary2->vid, $vocabulary1->vid));
- $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, 'Vocabulary loaded successfully by ID.');
- $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, 'Vocabulary loaded successfully by ID.');
- $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by ID.');
-
- $vocabulary = current(taxonomy_vocabulary_load_multiple(array(), array('name' => $vocabulary1->name)));
- $this->assertEqual($vocabulary->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by name.');
-
- $this->assertEqual(current(taxonomy_vocabulary_load_multiple(array($vocabulary1->vid), array('name' => $vocabulary1->name)))->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by name and ID.');
- }
-
- function testTaxonomyVocabularyChangeMachineName() {
-
- $field = array(
- 'field_name' => 'field_test',
- 'type' => 'test_field',
- );
- field_create_field($field);
- $instance = array(
- 'field_name' => 'field_test',
- 'entity_type' => 'taxonomy_term',
- 'bundle' => $this->vocabulary->machine_name,
- );
- field_create_instance($instance);
-
- $new_name = drupal_strtolower($this->randomName());
- $this->vocabulary->machine_name = $new_name;
- taxonomy_vocabulary_save($this->vocabulary);
-
- $this->assertTrue(field_info_instance('taxonomy_term', 'field_test', $new_name), 'The bundle name was updated correctly.');
- }
-
- function testUninstallReinstall() {
-
-
- $this->field_name = drupal_strtolower($this->randomName() . '_field_name');
- $this->field = array('field_name' => $this->field_name, 'type' => 'text', 'cardinality' => 4);
- $this->field = field_create_field($this->field);
- $this->instance = array(
- 'field_name' => $this->field_name,
- 'entity_type' => 'taxonomy_term',
- 'bundle' => $this->vocabulary->machine_name,
- 'label' => $this->randomName() . '_label',
- );
- field_create_instance($this->instance);
- module_disable(array('taxonomy'));
- require_once DRUPAL_ROOT . '/includes/install.inc';
- drupal_uninstall_modules(array('taxonomy'));
- module_enable(array('taxonomy'));
-
-
-
-
- unset($this->vocabulary->vid);
- taxonomy_vocabulary_save($this->vocabulary);
- unset($this->field['id']);
- field_create_field($this->field);
- field_create_instance($this->instance);
- }
- }
- class TaxonomyTermFunctionTestCase extends TaxonomyWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy term unit tests',
- 'description' => 'Unit tests for taxonomy term functions.',
- 'group' => 'Taxonomy',
- );
- }
- function testTermDelete() {
- $vocabulary = $this->createVocabulary();
- $valid_term = $this->createTerm($vocabulary);
-
- taxonomy_term_delete($valid_term->tid);
- $terms = taxonomy_term_load_multiple(array(), array('vid' => $vocabulary->vid));
- $this->assertTrue(empty($terms), 'Vocabulary is empty after deletion.');
-
- taxonomy_term_delete(42);
- }
-
- function testTaxonomyVocabularyTree() {
-
- $vocabulary = $this->createVocabulary();
- $term = array();
- for ($i = 0; $i < 6; $i++) {
- $term[$i] = $this->createTerm($vocabulary);
- }
-
- $term[2]->parent = array($term[1]->tid, $term[5]->tid);
- taxonomy_term_save($term[2]);
-
- $term[3]->parent = array($term[2]->tid);
- taxonomy_term_save($term[3]);
-
- $term[5]->parent = array($term[4]->tid);
- taxonomy_term_save($term[5]);
-
-
- $tree = taxonomy_get_tree($vocabulary->vid, $term[1]->tid, 1);
- $this->assertEqual(1, count($tree), 'We have one parent with depth 1.');
-
- $tree = taxonomy_get_tree($vocabulary->vid);
- $this->assertEqual(8, count($tree), 'We have all vocabulary tree elements.');
-
- foreach ($tree as $element) {
- if (!isset($depth_count[$element->depth])) {
- $depth_count[$element->depth] = 0;
- }
- $depth_count[$element->depth]++;
- }
- $this->assertEqual(3, $depth_count[0], 'Three elements in taxonomy tree depth 0.');
- $this->assertEqual(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.');
- $this->assertEqual(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.');
- $this->assertEqual(1, $depth_count[3], 'One element in taxonomy tree depth 3.');
- }
- }
- class TaxonomyLegacyTestCase extends TaxonomyWebTestCase {
- 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('taxonomy');
- $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access'));
- $this->drupalLogin($this->admin_user);
- }
-
- function testTaxonomyLegacyNode() {
-
- $langcode = LANGUAGE_NONE;
- $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'));
-
- $node = $this->drupalGetNodeByTitle($edit['title']);
- $this->assertEqual($node->created, strtotime($edit['date']), 'Legacy node was saved with the right date.');
- }
- }
- class TaxonomyTermTestCase extends TaxonomyWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy term functions and forms.',
- 'description' => 'Test load, save and delete for taxonomy terms.',
- 'group' => 'Taxonomy',
- );
- }
- function setUp() {
- parent::setUp('taxonomy');
- $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
- $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);
- }
-
- function testTaxonomyTermHierarchy() {
-
- $term1 = $this->createTerm($this->vocabulary);
- $term2 = $this->createTerm($this->vocabulary);
-
- $vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
- $this->assertEqual(0, $vocabulary->hierarchy, 'Vocabulary is flat.');
-
- $edit = array();
- $edit['parent[]'] = array($term1->tid);
- $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));
-
- $children = taxonomy_get_children($term1->tid);
- $parents = taxonomy_get_parents($term2->tid);
- $this->assertTrue(isset($children[$term2->tid]), 'Child found correctly.');
- $this->assertTrue(isset($parents[$term1->tid]), 'Parent found correctly.');
-
- $term = taxonomy_term_load($term2->tid);
- taxonomy_term_save($term);
- $parents = taxonomy_get_parents($term2->tid);
- $this->assertTrue(isset($parents[$term1->tid]), 'Parent found correctly.');
-
- $term3 = $this->createTerm($this->vocabulary);
- $term2->parent = array($term1->tid, $term3->tid);
- taxonomy_term_save($term2);
- $parents = taxonomy_get_parents($term2->tid);
- $this->assertTrue(isset($parents[$term1->tid]) && isset($parents[$term3->tid]), 'Both parents found successfully.');
- }
-
- function testTaxonomyNode() {
-
- $term1 = $this->createTerm($this->vocabulary);
- $term2 = $this->createTerm($this->vocabulary);
-
- $edit = array();
- $langcode = LANGUAGE_NONE;
- $edit["title"] = $this->randomName();
- $edit["body[$langcode][0][value]"] = $this->randomName();
- $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid;
- $this->drupalPost('node/add/article', $edit, t('Save'));
-
- $node = $this->drupalGetNodeByTitle($edit["title"]);
- $this->drupalGet('node/' . $node->nid);
- $this->assertText($term1->name, 'Term is displayed when viewing the node.');
-
- $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term2->tid;
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
- $this->drupalGet('node/' . $node->nid);
- $this->assertText($term2->name, 'Term is displayed when viewing the node.');
-
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Preview'));
- $this->assertNoUniqueText($term2->name, 'Term is displayed when previewing the node.');
- $this->drupalPost(NULL, NULL, t('Preview'));
- $this->assertNoUniqueText($term2->name, 'Term is displayed when previewing the node again.');
- }
-
- function testNodeTermCreationAndDeletion() {
-
- $instance = $this->instance;
- $instance['widget'] = array('type' => 'taxonomy_autocomplete');
- $instance['bundle'] = 'page';
- field_create_instance($instance);
- $terms = array(
- 'term1' => $this->randomName(),
- 'term2' => $this->randomName() . ', ' . $this->randomName(),
- 'term3' => $this->randomName(),
- );
- $edit = array();
- $langcode = LANGUAGE_NONE;
- $edit["title"] = $this->randomName();
- $edit["body[$langcode][0][value]"] = $this->randomName();
-
-
- $edit[$instance['field_name'] . "[$langcode]"] = drupal_implode_tags($terms);
-
- $this->drupalPost('node/add/page', $edit, t('Preview'));
- foreach ($terms as $term) {
- $this->assertText($term, 'The term appears on the node preview.');
- }
- $tree = taxonomy_get_tree($this->vocabulary->vid);
- $this->assertTrue(empty($tree), 'The terms are not created on preview.');
-
- drupal_static_reset();
-
- $this->drupalPost('node/add/page', $edit, t('Save'));
- $this->assertRaw(t('@type %title has been created.', array('@type' => t('Basic page'), '%title' => $edit["title"])), 'The node was created successfully.');
- foreach ($terms as $term) {
- $this->assertText($term, 'The term was saved and appears on the node page.');
- }
-
- $term_objects = array();
- foreach ($terms as $key => $term) {
- $term_objects[$key] = taxonomy_get_term_by_name($term);
- $term_objects[$key] = reset($term_objects[$key]);
- }
-
- $node = $this->drupalGetNodeByTitle($edit["title"]);
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
- foreach ($terms as $term) {
- $this->assertText($term, 'The term was retained after edit and still appears on the node page.');
- }
-
- $this->drupalPost('taxonomy/term/' . $term_objects['term1']->tid . '/edit', array(), t('Delete'));
- $this->drupalPost(NULL, NULL, t('Delete'));
- $term_names = array($term_objects['term2']->name, $term_objects['term3']->name);
-
- $this->drupalGet('node/' . $node->nid);
- foreach ($term_names as $term_name) {
- $this->assertText($term_name, format_string('The term %name appears on the node page after one term %deleted was deleted', array('%name' => $term_name, '%deleted' => $term_objects['term1']->name)));
- }
- $this->assertNoText($term_objects['term1']->name, format_string('The deleted term %name does not appear on the node page.', array('%name' => $term_objects['term1']->name)));
-
-
- $input = substr($term_objects['term2']->name, 0, 3);
- $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
- $this->assertRaw('{"\u0022' . $term_objects['term2']->name . '\u0022":"' . $term_objects['term2']->name . '"}', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term2']->name)));
-
-
- $input = substr($term_objects['term3']->name, 0, 3);
- $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
- $this->assertRaw('{"' . $term_objects['term3']->name . '":"' . $term_objects['term3']->name . '"}', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term3']->name)));
-
- $field_name = $this->randomName();
- $tag = $this->randomName();
- $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name));
- $this->assertFalse(field_info_field($field_name), format_string('Field %field_name does not exist.', array('%field_name' => $field_name)));
- $this->drupalGet('taxonomy/autocomplete/' . $field_name . '/' . $tag);
- $this->assertRaw($message, 'Autocomplete returns correct error message when the taxonomy field does not exist.');
-
-
- $field_name = '';
- $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name));
- $this->drupalGet('taxonomy/autocomplete');
- $this->assertRaw($message, 'Autocomplete returns correct error message when no taxonomy field is given.');
- }
-
- function testTermAutocompletion() {
-
- $first_term = $this->createTerm($this->vocabulary);
- $first_term->name = '10/16/2011';
- taxonomy_term_save($first_term);
-
- $second_term = $this->createTerm($this->vocabulary);
- $second_term->name = '10/17/2011';
- taxonomy_term_save($second_term);
-
- $third_term = $this->createTerm($this->vocabulary);
- $third_term->name = 'term with, a comma and / a slash';
- taxonomy_term_save($third_term);
-
-
- $input = '10/';
- $path = 'taxonomy/autocomplete/taxonomy_';
- $path .= $this->vocabulary->machine_name . '/' . $input;
-
- $url = url($path, array('absolute' => TRUE));
- $result = drupal_http_request($url);
- $data = drupal_json_decode($result->data);
- $this->assertEqual($data[$first_term->name], check_plain($first_term->name), 'Autocomplete returned the first matching term.');
- $this->assertEqual($data[$second_term->name], check_plain($second_term->name), 'Autocomplete returned the second matching term.');
-
-
- $input = '10/16';
- $url = 'taxonomy/autocomplete/taxonomy_';
- $url .= $this->vocabulary->machine_name . '/' . $input;
- $this->drupalGet($url);
- $target = array($first_term->name => check_plain($first_term->name));
- $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns only the expected matching term.');
-
- $input = '"term with, comma and / a';
- $url = 'taxonomy/autocomplete/taxonomy_';
- $url .= $this->vocabulary->machine_name . '/' . $input;
- $this->drupalGet($url);
- $n = $third_term->name;
-
- if (strpos($third_term->name, ',') !== FALSE || strpos($third_term->name, '"') !== FALSE) {
- $n = '"' . str_replace('"', '""', $third_term->name) . '"';
- }
- $target = array($n => check_plain($third_term->name));
- $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns a term containing a comma and a slash.');
- }
-
- function testTermInterface() {
- $edit = array(
- 'name' => $this->randomName(12),
- 'description[value]' => $this->randomName(100),
- );
-
-
- $edit['parent[]'] = array(0);
-
- $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
- $terms = taxonomy_get_term_by_name($edit['name']);
- $term = reset($terms);
- $this->assertNotNull($term, 'Term found in database.');
-
- $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
-
-
-
- $this->clickLink(t('edit'));
- $this->assertRaw($edit['name'], 'The randomly generated term name is present.');
- $this->assertText($edit['description[value]'], 'The randomly generated term description is present.');
- $edit = array(
- 'name' => $this->randomName(14),
- 'description[value]' => $this->randomName(102),
- );
-
- $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
-
- $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
- $this->assertText($edit['name'], 'The randomly generated term name is present.');
- $this->assertLink(t('edit'));
-
- $this->drupalGet('taxonomy/term/' . $term->tid);
- $this->assertText($edit['name'], 'The randomly generated term name is present.');
- $this->assertText($edit['description[value]'], 'The randomly generated term description is present.');
-
- $this->assertPattern('|class="taxonomy-term-description"|', 'Term page displayed the term description element.');
-
- $term->description = '';
- taxonomy_term_save($term);
- $this->drupalGet('taxonomy/term/' . $term->tid);
- $this->assertNoPattern('|class="taxonomy-term-description"|', 'Term page did not display the term description when description was blank.');
-
- $this->drupalGet('taxonomy/term/' . $term->tid . '/feed');
-
-
- $this->drupalGet('taxonomy/term/' . $term->tid . '/edit/' . $this->randomName());
-
- $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', array(), t('Delete'));
- $this->drupalPost(NULL, NULL, t('Delete'));
-
- $this->drupalGet('taxonomy/term/' . $term->tid);
- $this->assertResponse(404, 'The taxonomy term page was not found.');
- }
-
- function testTermReorder() {
- $this->createTerm($this->vocabulary);
- $this->createTerm($this->vocabulary);
- $this->createTerm($this->vocabulary);
-
-
- drupal_static_reset('taxonomy_get_tree');
- drupal_static_reset('taxonomy_get_treeparent');
- drupal_static_reset('taxonomy_get_treeterms');
- list($term1, $term2, $term3) = taxonomy_get_tree($this->vocabulary->vid);
- $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
-
-
-
-
- $edit = array(
- 'tid:' . $term2->tid . ':0[tid]' => $term2->tid,
- 'tid:' . $term2->tid . ':0[parent]' => 0,
- 'tid:' . $term2->tid . ':0[depth]' => 0,
- 'tid:' . $term2->tid . ':0[weight]' => 0,
- 'tid:' . $term3->tid . ':0[tid]' => $term3->tid,
- 'tid:' . $term3->tid . ':0[parent]' => $term2->tid,
- 'tid:' . $term3->tid . ':0[depth]' => 1,
- 'tid:' . $term3->tid . ':0[weight]' => 1,
- 'tid:' . $term1->tid . ':0[tid]' => $term1->tid,
- 'tid:' . $term1->tid . ':0[parent]' => 0,
- 'tid:' . $term1->tid . ':0[depth]' => 0,
- 'tid:' . $term1->tid . ':0[weight]' => 2,
- );
- $this->drupalPost(NULL, $edit, t('Save'));
- drupal_static_reset('taxonomy_get_tree');
- drupal_static_reset('taxonomy_get_treeparent');
- drupal_static_reset('taxonomy_get_treeterms');
- $terms = taxonomy_get_tree($this->vocabulary->vid);
- $this->assertEqual($terms[0]->tid, $term2->tid, 'Term 2 was moved above term 1.');
- $this->assertEqual($terms[1]->parents, array($term2->tid), 'Term 3 was made a child of term 2.');
- $this->assertEqual($terms[2]->tid, $term1->tid, 'Term 1 was moved below term 2.');
- $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name, array(), t('Reset to alphabetical'));
-
- $this->drupalPost(NULL, array(), t('Reset to alphabetical'));
- drupal_static_reset('taxonomy_get_tree');
- drupal_static_reset('taxonomy_get_treeparent');
- drupal_static_reset('taxonomy_get_treeterms');
- $terms = taxonomy_get_tree($this->vocabulary->vid);
- $this->assertEqual($terms[0]->tid, $term1->tid, 'Term 1 was moved to back above term 2.');
- $this->assertEqual($terms[1]->tid, $term2->tid, 'Term 2 was moved to back below term 1.');
- $this->assertEqual($terms[2]->tid, $term3->tid, 'Term 3 is still below term 2.');
- $this->assertEqual($terms[2]->parents, array($term2->tid), 'Term 3 is still a child of term 2.' . var_export($terms[1]->tid, 1));
- }
-
- function testTermMultipleParentsInterface() {
-
- $parent = $this->createTerm($this->vocabulary);
-
- $edit = array(
- 'name' => $this->randomName(12),
- 'description[value]' => $this->randomName(100),
- 'parent[]' => array(0, $parent->tid),
- );
-
- $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
-
- $terms = taxonomy_get_term_by_name($edit['name']);
- $term = reset($terms);
- $this->assertNotNull($term, 'Term found in database.');
- $this->assertEqual($edit['name'], $term->name, 'Term name was successfully saved.');
- $this->assertEqual($edit['description[value]'], $term->description, 'Term description was successfully saved.');
-
-
- $parents = taxonomy_get_parents($term->tid);
- $parent = reset($parents);
- $this->assertEqual($edit['parent[]'][1], $parent->tid, 'Term parents were successfully saved.');
- }
-
- function testTaxonomyGetTermByName() {
- $term = $this->createTerm($this->vocabulary);
-
- $terms = taxonomy_get_term_by_name($term->name);
- $this->assertTrue(isset($terms[$term->tid]), 'Term loaded using exact name.');
-
- $terms = taxonomy_get_term_by_name(' ' . $term->name . ' ');
- $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with extra whitespace.');
-
- $terms = taxonomy_get_term_by_name(strtoupper($term->name));
- $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with uppercased name.');
-
- $terms = taxonomy_get_term_by_name(strtolower($term->name));
- $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with lowercased name.');
-
- $terms = taxonomy_get_term_by_name('Banana');
- $this->assertFalse($terms);
-
- $terms = taxonomy_get_term_by_name(drupal_substr($term->name, 2));
- $this->assertFalse($terms);
-
- $new_vocabulary = $this->createVocabulary();
- $new_term = new stdClass();
- $new_term->name = $term->name;
- $new_term->vid = $new_vocabulary->vid;
- taxonomy_term_save($new_term);
-
- $terms = taxonomy_get_term_by_name($term->name);
- $this->assertEqual(count($terms), 2, 'Two terms loaded with the same name.');
-
- $terms = taxonomy_get_term_by_name($term->name, $this->vocabulary->machine_name);
- $this->assertEqual(count($terms), 1, 'One term loaded when restricted by vocabulary.');
- $this->assertTrue(isset($terms[$term->tid]), 'Term loaded using exact name and vocabulary machine name.');
-
- $term2 = $this->createTerm($this->vocabulary);
-
-
- $terms = taxonomy_get_term_by_name($term2->name, $new_vocabulary->machine_name);
- $this->assertFalse($terms, 'Invalid term name restricted by vocabulary machine name not loaded.');
-
- $terms = taxonomy_get_term_by_name($term2->name, 'non_existing_vocabulary');
- $this->assertEqual(count($terms), 0, 'No terms loaded when restricted by a non-existing vocabulary.');
- }
- }
- class TaxonomyRSSTestCase extends TaxonomyWebTestCase {
- 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('taxonomy');
- $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);
- }
-
- function testTaxonomyRSS() {
-
- $term1 = $this->createTerm($this->vocabulary);
-
- $this->drupalGet("admin/structure/types/manage/article/display");
- $edit = array(
- "view_modes_custom[rss]" => '1',
- );
- $this->drupalPost(NULL, $edit, t('Save'));
-
- $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'));
-
- $edit = array();
- $langcode = LANGUAGE_NONE;
- $edit["title"] = $this->randomName();
- $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid;
- $this->drupalPost('node/add/article', $edit, t('Save'));
-
- $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.');
- }
- }
- class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy term index',
- 'description' => 'Tests the hook implementations that maintain the taxonomy index.',
- 'group' => 'Taxonomy',
- );
- }
- function setUp() {
- parent::setUp('taxonomy');
-
- $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
- $this->drupalLogin($this->admin_user);
-
- $this->vocabulary = $this->createVocabulary();
- $this->field_name_1 = drupal_strtolower($this->randomName());
- $this->field_1 = array(
- 'field_name' => $this->field_name_1,
- 'type' => 'taxonomy_term_reference',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'settings' => array(
- 'allowed_values' => array(
- array(
- 'vocabulary' => $this->vocabulary->machine_name,
- 'parent' => 0,
- ),
- ),
- ),
- );
- field_create_field($this->field_1);
- $this->instance_1 = array(
- 'field_name' => $this->field_name_1,
- 'bundle' => 'article',
- 'entity_type' => 'node',
- 'widget' => array(
- 'type' => 'options_select',
- ),
- 'display' => array(
- 'default' => array(
- 'type' => 'taxonomy_term_reference_link',
- ),
- ),
- );
- field_create_instance($this->instance_1);
- $this->field_name_2 = drupal_strtolower($this->randomName());
- $this->field_2 = array(
- 'field_name' => $this->field_name_2,
- 'type' => 'taxonomy_term_reference',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'settings' => array(
- 'allowed_values' => array(
- array(
- 'vocabulary' => $this->vocabulary->machine_name,
- 'parent' => 0,
- ),
- ),
- ),
- );
- field_create_field($this->field_2);
- $this->instance_2 = array(
- 'field_name' => $this->field_name_2,
- 'bundle' => 'article',
- 'entity_type' => 'node',
- 'widget' => array(
- 'type' => 'options_select',
- ),
- 'display' => array(
- 'default' => array(
- 'type' => 'taxonomy_term_reference_link',
- ),
- ),
- );
- field_create_instance($this->instance_2);
- }
-
- function testTaxonomyIndex() {
-
- $term_1 = $this->createTerm($this->vocabulary);
- $term_2 = $this->createTerm($this->vocabulary);
-
- $edit = array();
- $langcode = LANGUAGE_NONE;
- $edit["title"] = $this->randomName();
- $edit["body[$langcode][0][value]"] = $this->randomName();
- $edit["{$this->field_name_1}[$langcode][]"] = $term_1->tid;
- $edit["{$this->field_name_2}[$langcode][]"] = $term_1->tid;
- $this->drupalPost('node/add/article', $edit, t('Save'));
-
- $node = $this->drupalGetNodeByTitle($edit["title"]);
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_1->tid,
- ))->fetchField();
- $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
-
- $edit["{$this->field_name_1}[$langcode][]"] = $term_2->tid;
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_1->tid,
- ))->fetchField();
- $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_2->tid,
- ))->fetchField();
- $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
-
- $edit["{$this->field_name_2}[$langcode][]"] = $term_2->tid;
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_1->tid,
- ))->fetchField();
- $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_2->tid,
- ))->fetchField();
- $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
-
- $update_node = array(
- 'nid' => $node->nid,
- 'vid' => $node->vid,
- 'uid' => $node->uid,
- 'type' => $node->type,
- 'title' => $this->randomName(),
- );
-
- $updated_node = (object) $update_node;
- node_save($updated_node);
-
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_1->tid,
- ))->fetchField();
- $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_2->tid,
- ))->fetchField();
- $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
-
- $update_node[$this->field_name_1][$langcode] = array(array('tid' => $term_1->tid));
- $updated_node = (object) $update_node;
- node_save($updated_node);
-
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_1->tid,
- ))->fetchField();
- $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_2->tid,
- ))->fetchField();
- $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
-
- $update_node[$this->field_name_2][$langcode] = array(array('tid' => $term_1->tid));
- $updated_node = (object) $update_node;
- node_save($updated_node);
-
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_1->tid,
- ))->fetchField();
- $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
- $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
- ':nid' => $node->nid,
- ':tid' => $term_2->tid,
- ))->fetchField();
- $this->assertEqual(0, $index_count, 'Term 2 is not indexed.');
- }
-
- function testTaxonomyTermHierarchyBreadcrumbs() {
-
- $term1 = $this->createTerm($this->vocabulary);
- $term2 = $this->createTerm($this->vocabulary);
- $term1->parent = array($term2->tid);
- taxonomy_term_save($term1);
-
- $this->drupalGet('taxonomy/term/' . $term1->tid);
- $this->assertRaw(l($term2->name, 'taxonomy/term/' . $term2->tid), 'Parent term link is displayed when viewing the node.');
- }
- }
- class TaxonomyLoadMultipleTestCase extends TaxonomyWebTestCase {
- 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);
- }
-
- function testTaxonomyTermMultipleLoad() {
-
- $vocabulary = $this->createVocabulary();
-
- $i = 0;
- while ($i < 5) {
- $i++;
- $this->createTerm($vocabulary);
- }
-
- $terms = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
- $count = count($terms);
- $this->assertEqual($count, 5, format_string('Correct number of terms were loaded. !count terms.', array('!count' => $count)));
-
- $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.');
-
- $terms3 = taxonomy_term_load_multiple(array_keys($terms2), array('vid' => $vocabulary->vid));
- $this->assertEqual($terms2, $terms3);
-
- $deleted = array_shift($terms3);
- taxonomy_term_delete($deleted->tid);
- $deleted_term = taxonomy_term_load($deleted->tid);
- $this->assertFalse($deleted_term);
-
- $terms4 = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
- $this->assertEqual(count($terms4), 4, 'Correct number of terms were loaded.');
- $this->assertFalse(isset($terms4[$deleted->tid]));
-
- $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.');
- }
- }
- class TaxonomyHooksTestCase extends TaxonomyWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy term hooks',
- 'description' => 'Hooks for taxonomy term load/save/delete.',
- 'group' => 'Taxonomy',
- );
- }
- function setUp() {
- parent::setUp('taxonomy', 'taxonomy_test');
- module_load_include('inc', 'taxonomy', 'taxonomy.pages');
- $taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
- $this->drupalLogin($taxonomy_admin);
- }
-
- function testTaxonomyTermHooks() {
- $vocabulary = $this->createVocabulary();
-
- $edit = array(
- 'name' => $this->randomName(),
- 'antonym' => 'Long',
- );
- $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save'));
- $terms = taxonomy_get_term_by_name($edit['name']);
- $term = reset($terms);
- $this->assertEqual($term->antonym, $edit['antonym'], 'Antonym was loaded into the term object.');
-
- $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.');
-
-
- $term = taxonomy_term_load($term->tid);
- $term_build = taxonomy_term_page($term);
- $this->assertFalse(empty($term_build['term_heading']['term']['taxonomy_test_term_view_check']), 'hook_taxonomy_term_view() was invoked when viewing the term.');
- $this->assertFalse(empty($term_build['term_heading']['term']['taxonomy_test_entity_view_check']), 'hook_entity_view() was invoked when viewing 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.');
- }
- }
- class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase {
- protected $instance;
- protected $vocabulary;
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy term reference field',
- 'description' => 'Test the creation of term fields.',
- '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->vocabulary = $this->createVocabulary();
-
- $this->field_name = drupal_strtolower($this->randomName());
- $this->field = array(
- 'field_name' => $this->field_name,
- 'type' => 'taxonomy_term_reference',
- 'settings' => array(
- 'allowed_values' => array(
- array(
- 'vocabulary' => $this->vocabulary->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);
- }
-
- function testTaxonomyTermFieldValidation() {
-
- $langcode = LANGUAGE_NONE;
- $entity = field_test_create_stub_entity();
- $term = $this->createTerm($this->vocabulary);
- $entity->{$this->field_name}[$langcode][0]['tid'] = $term->tid;
- try {
- field_attach_validate('test_entity', $entity);
- $this->pass('Correct term does not cause validation error.');
- }
- catch (FieldValidationException $e) {
- $this->fail('Correct term does not cause validation error.');
- }
- $entity = field_test_create_stub_entity();
- $bad_term = $this->createTerm($this->createVocabulary());
- $entity->{$this->field_name}[$langcode][0]['tid'] = $bad_term->tid;
- try {
- field_attach_validate('test_entity', $entity);
- $this->fail('Wrong term causes validation error.');
- }
- catch (FieldValidationException $e) {
- $this->pass('Wrong term causes validation error.');
- }
- }
-
- function testTaxonomyTermFieldWidgets() {
-
- $term = $this->createTerm($this->vocabulary);
-
- $langcode = LANGUAGE_NONE;
- $this->drupalGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$this->field_name}[$langcode]", '', 'Widget is displayed.');
-
- $edit = array(
- "{$this->field_name}[$langcode]" => array($term->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.');
-
- $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($term->name, 'Term name is displayed.');
-
- taxonomy_vocabulary_delete($this->vocabulary->vid);
- $this->drupalGet('test-entity/add/test-bundle');
- $this->assertNoFieldByName("{$this->field_name}[$langcode]", '', 'Widget is not displayed.');
- }
-
- function testTaxonomyTermFieldChangeMachineName() {
-
-
- $this->field['settings']['allowed_values'] = array(
- array(
- 'vocabulary' => $this->vocabulary->machine_name,
- 'parent' => '0',
- ),
- array(
- 'vocabulary' => $this->vocabulary->machine_name,
- 'parent' => '0',
- ),
- array(
- 'vocabulary' => 'foo',
- 'parent' => '0',
- ),
- );
- field_update_field($this->field);
-
- $old_name = $this->vocabulary->machine_name;
- $new_name = drupal_strtolower($this->randomName());
- $this->vocabulary->machine_name = $new_name;
- taxonomy_vocabulary_save($this->vocabulary);
-
- $info = entity_get_info('taxonomy_term');
- $this->assertFalse(isset($info['bundles'][$old_name]), 'The old bundle name does not appear in entity_get_info().');
- $this->assertTrue(isset($info['bundles'][$new_name]), 'The new bundle name appears in entity_get_info().');
-
- $field = field_info_field($this->field_name);
- $allowed_values = $field['settings']['allowed_values'];
- $this->assertEqual($allowed_values[0]['vocabulary'], $new_name, 'Index 0: Machine name was updated correctly.');
- $this->assertEqual($allowed_values[1]['vocabulary'], $new_name, 'Index 1: Machine name was updated correctly.');
- $this->assertEqual($allowed_values[2]['vocabulary'], 'foo', 'Index 2: Machine name was left untouched.');
- }
- }
- class TaxonomyTermFieldMultipleVocabularyTestCase extends TaxonomyWebTestCase {
- 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();
-
- $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);
- }
-
- function testTaxonomyTermFieldMultipleVocabularies() {
-
- $term1 = $this->createTerm($this->vocabulary1);
- $term2 = $this->createTerm($this->vocabulary2);
-
- $langcode = LANGUAGE_NONE;
- $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.');
-
- $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.');
-
- taxonomy_vocabulary_delete($this->vocabulary2->vid);
-
- $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);
-
- $this->assertText($term1->name, 'Term 1 name is displayed.');
- $this->assertNoText($term2->name, 'Term 2 name is not displayed.');
-
- $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.');
-
- $this->drupalGet('test-entity/add/test-bundle');
- $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is still displayed.');
-
- $edit = array(
- "{$this->field_name}[$langcode][]" => array($term1->tid),
- );
- $this->drupalPost(NULL, $edit, t('Save'));
- }
- }
- class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy token replacement',
- 'description' => 'Generates text using placeholders for dummy content to check taxonomy token replacement.',
- 'group' => 'Taxonomy',
- );
- }
- function setUp() {
- parent::setUp();
- $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
- $this->drupalLogin($this->admin_user);
- $this->vocabulary = $this->createVocabulary();
- $this->langcode = LANGUAGE_NONE;
- $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);
- }
-
- function testTaxonomyTokenReplacement() {
- global $language;
-
- $term1 = $this->createTerm($this->vocabulary);
- $term2 = $this->createTerm($this->vocabulary);
-
- $edit = array();
- $edit['name'] = '<blink>Blinking Text</blink>';
- $edit['parent[]'] = array($term1->tid);
- $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));
-
- $edit = array();
- $node = $this->drupalCreateNode(array('type' => 'article'));
- $edit[$this->instance['field_name'] . '[' . $this->langcode . '][]'] = $term2->tid;
- $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- $tests = array();
- $tests['[term:tid]'] = $term1->tid;
- $tests['[term:name]'] = check_plain($term1->name);
- $tests['[term:description]'] = check_markup($term1->description, $term1->format);
- $tests['[term:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE));
- $tests['[term:node-count]'] = 0;
- $tests['[term:parent:name]'] = '[term:parent:name]';
- $tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name);
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('term' => $term1), array('language' => $language));
- $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', array('%token' => $input)));
- }
-
- $tests = array();
- $tests['[term:tid]'] = $term2->tid;
- $tests['[term:name]'] = check_plain($term2->name);
- $tests['[term:description]'] = check_markup($term2->description, $term2->format);
- $tests['[term:url]'] = url('taxonomy/term/' . $term2->tid, array('absolute' => TRUE));
- $tests['[term:node-count]'] = 1;
- $tests['[term:parent:name]'] = check_plain($term1->name);
- $tests['[term:parent:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE));
- $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]';
- $tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name);
-
- $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('term' => $term2), array('language' => $language));
- $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', array('%token' => $input)));
- }
-
- $tests['[term:name]'] = $term2->name;
- $tests['[term:description]'] = $term2->description;
- $tests['[term:parent:name]'] = $term1->name;
- $tests['[term:vocabulary:name]'] = $this->vocabulary->name;
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('term' => $term2), array('language' => $language, 'sanitize' => FALSE));
- $this->assertEqual($output, $expected, format_string('Unsanitized taxonomy term token %token replaced.', array('%token' => $input)));
- }
-
- $tests = array();
- $tests['[vocabulary:vid]'] = $this->vocabulary->vid;
- $tests['[vocabulary:name]'] = check_plain($this->vocabulary->name);
- $tests['[vocabulary:description]'] = filter_xss($this->vocabulary->description);
- $tests['[vocabulary:node-count]'] = 1;
- $tests['[vocabulary:term-count]'] = 2;
-
- $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language));
- $this->assertEqual($output, $expected, format_string('Sanitized taxonomy vocabulary token %token replaced.', array('%token' => $input)));
- }
-
- $tests['[vocabulary:name]'] = $this->vocabulary->name;
- $tests['[vocabulary:description]'] = $this->vocabulary->description;
- foreach ($tests as $input => $expected) {
- $output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language, 'sanitize' => FALSE));
- $this->assertEqual($output, $expected, format_string('Unsanitized taxonomy vocabulary token %token replaced.', array('%token' => $input)));
- }
- }
- }
- class TaxonomyThemeTestCase extends TaxonomyWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Taxonomy theme switching',
- 'description' => 'Verifies that various taxonomy pages use the expected theme.',
- 'group' => 'Taxonomy',
- );
- }
- function setUp() {
- parent::setUp();
-
-
- variable_set('theme_default', 'bartik');
- variable_set('admin_theme', 'seven');
-
-
- $admin_user = $this->drupalCreateUser(array('administer taxonomy', 'view the administration theme'));
- $this->drupalLogin($admin_user);
- }
-
- function testTaxonomyTermThemes() {
-
-
- $vocabulary = $this->createVocabulary();
- $this->drupalGet('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add');
- $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page for adding a taxonomy term.");
-
- $term = $this->createTerm($vocabulary);
- $this->drupalGet('taxonomy/term/' . $term->tid);
- $this->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page for viewing a taxonomy term.");
-
- $this->drupalGet('taxonomy/term/' . $term->tid . '/edit');
- $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page for editing a taxonomy term.");
- }
- }
- class TaxonomyEFQTestCase extends TaxonomyWebTestCase {
- 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();
- }
-
- 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.');
-
- $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)));
- }
- }
|