taxonomy.test 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. <?php
  2. /**
  3. * @file
  4. * Tests for taxonomy.module.
  5. */
  6. /**
  7. * Provides common helper methods for Taxonomy module tests.
  8. */
  9. class TaxonomyWebTestCase extends DrupalWebTestCase {
  10. /**
  11. * Returns a new vocabulary with random properties.
  12. */
  13. function createVocabulary() {
  14. // Create a vocabulary.
  15. $vocabulary = new stdClass();
  16. $vocabulary->name = $this->randomName();
  17. $vocabulary->description = $this->randomName();
  18. $vocabulary->machine_name = drupal_strtolower($this->randomName());
  19. $vocabulary->help = '';
  20. $vocabulary->nodes = array('article' => 'article');
  21. $vocabulary->weight = mt_rand(0, 10);
  22. taxonomy_vocabulary_save($vocabulary);
  23. return $vocabulary;
  24. }
  25. /**
  26. * Returns a new term with random properties in vocabulary $vid.
  27. */
  28. function createTerm($vocabulary) {
  29. $term = new stdClass();
  30. $term->name = $this->randomName();
  31. $term->description = $this->randomName();
  32. // Use the first available text format.
  33. $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
  34. $term->vid = $vocabulary->vid;
  35. taxonomy_term_save($term);
  36. return $term;
  37. }
  38. }
  39. /**
  40. * Tests the taxonomy vocabulary interface.
  41. */
  42. class TaxonomyVocabularyFunctionalTest extends TaxonomyWebTestCase {
  43. public static function getInfo() {
  44. return array(
  45. 'name' => 'Taxonomy vocabulary interface',
  46. 'description' => 'Test the taxonomy vocabulary interface.',
  47. 'group' => 'Taxonomy',
  48. );
  49. }
  50. function setUp() {
  51. parent::setUp();
  52. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
  53. $this->drupalLogin($this->admin_user);
  54. $this->vocabulary = $this->createVocabulary();
  55. }
  56. /**
  57. * Create, edit and delete a vocabulary via the user interface.
  58. */
  59. function testVocabularyInterface() {
  60. // Visit the main taxonomy administration page.
  61. $this->drupalGet('admin/structure/taxonomy');
  62. // Create a new vocabulary.
  63. $this->clickLink(t('Add vocabulary'));
  64. $edit = array();
  65. $machine_name = drupal_strtolower($this->randomName());
  66. $edit['name'] = $this->randomName();
  67. $edit['description'] = $this->randomName();
  68. $edit['machine_name'] = $machine_name;
  69. $this->drupalPost(NULL, $edit, t('Save'));
  70. $this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), 'Vocabulary created successfully.');
  71. // Edit the vocabulary.
  72. $this->drupalGet('admin/structure/taxonomy');
  73. $this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.');
  74. $this->clickLink(t('edit vocabulary'));
  75. $edit = array();
  76. $edit['name'] = $this->randomName();
  77. $this->drupalPost(NULL, $edit, t('Save'));
  78. $this->drupalGet('admin/structure/taxonomy');
  79. $this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.');
  80. // Try to submit a vocabulary with a duplicate machine name.
  81. $edit['machine_name'] = $machine_name;
  82. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  83. $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
  84. // Try to submit an invalid machine name.
  85. $edit['machine_name'] = '!&^%';
  86. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  87. $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  88. }
  89. /**
  90. * Changing weights on the vocabulary overview with two or more vocabularies.
  91. */
  92. function testTaxonomyAdminChangingWeights() {
  93. // Create some vocabularies.
  94. for ($i = 0; $i < 10; $i++) {
  95. $this->createVocabulary();
  96. }
  97. // Get all vocabularies and change their weights.
  98. $vocabularies = taxonomy_get_vocabularies();
  99. $edit = array();
  100. foreach ($vocabularies as $key => $vocabulary) {
  101. $vocabulary->weight = -$vocabulary->weight;
  102. $vocabularies[$key]->weight = $vocabulary->weight;
  103. $edit[$key . '[weight]'] = $vocabulary->weight;
  104. }
  105. // Saving the new weights via the interface.
  106. $this->drupalPost('admin/structure/taxonomy', $edit, t('Save'));
  107. // Load the vocabularies from the database.
  108. $new_vocabularies = taxonomy_get_vocabularies();
  109. // Check that the weights are saved in the database correctly.
  110. foreach ($vocabularies as $key => $vocabulary) {
  111. $this->assertEqual($new_vocabularies[$key]->weight, $vocabularies[$key]->weight, 'The vocabulary weight was changed.');
  112. }
  113. }
  114. /**
  115. * Test the vocabulary overview with no vocabularies.
  116. */
  117. function testTaxonomyAdminNoVocabularies() {
  118. // Delete all vocabularies.
  119. $vocabularies = taxonomy_get_vocabularies();
  120. foreach ($vocabularies as $key => $vocabulary) {
  121. taxonomy_vocabulary_delete($key);
  122. }
  123. // Confirm that no vocabularies are found in the database.
  124. $this->assertFalse(taxonomy_get_vocabularies(), 'No vocabularies found in the database.');
  125. $this->drupalGet('admin/structure/taxonomy');
  126. // Check the default message for no vocabularies.
  127. $this->assertText(t('No vocabularies available.'), 'No vocabularies were found.');
  128. }
  129. /**
  130. * Deleting a vocabulary.
  131. */
  132. function testTaxonomyAdminDeletingVocabulary() {
  133. // Create a vocabulary.
  134. $edit = array(
  135. 'name' => $this->randomName(),
  136. 'machine_name' => drupal_strtolower($this->randomName()),
  137. );
  138. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  139. $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
  140. // Check the created vocabulary.
  141. $vocabularies = taxonomy_get_vocabularies();
  142. $vid = $vocabularies[count($vocabularies) - 1]->vid;
  143. entity_get_controller('taxonomy_vocabulary')->resetCache();
  144. $vocabulary = taxonomy_vocabulary_load($vid);
  145. $this->assertTrue($vocabulary, 'Vocabulary found in database.');
  146. // Delete the vocabulary.
  147. $edit = array();
  148. $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/edit', $edit, t('Delete'));
  149. $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', array('%name' => $vocabulary->name)), '[confirm deletion] Asks for confirmation.');
  150. $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.');
  151. // Confirm deletion.
  152. $this->drupalPost(NULL, NULL, t('Delete'));
  153. $this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), 'Vocabulary deleted.');
  154. entity_get_controller('taxonomy_vocabulary')->resetCache();
  155. $this->assertFalse(taxonomy_vocabulary_load($vid), 'Vocabulary is not found in the database.');
  156. }
  157. }
  158. /**
  159. * Tests for taxonomy vocabulary functions.
  160. */
  161. class TaxonomyVocabularyTestCase extends TaxonomyWebTestCase {
  162. public static function getInfo() {
  163. return array(
  164. 'name' => 'Taxonomy vocabularies',
  165. 'description' => 'Test loading, saving and deleting vocabularies.',
  166. 'group' => 'Taxonomy',
  167. );
  168. }
  169. function setUp() {
  170. parent::setUp('taxonomy', 'field_test');
  171. $admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy'));
  172. $this->drupalLogin($admin_user);
  173. $this->vocabulary = $this->createVocabulary();
  174. }
  175. /**
  176. * Ensure that when an invalid vocabulary vid is loaded, it is possible
  177. * to load the same vid successfully if it subsequently becomes valid.
  178. */
  179. function testTaxonomyVocabularyLoadReturnFalse() {
  180. // Load a vocabulary that doesn't exist.
  181. $vocabularies = taxonomy_get_vocabularies();
  182. $vid = count($vocabularies) + 1;
  183. $vocabulary = taxonomy_vocabulary_load($vid);
  184. // This should not return an object because no such vocabulary exists.
  185. $this->assertTrue(empty($vocabulary), 'No object loaded.');
  186. // Create a new vocabulary.
  187. $this->createVocabulary();
  188. // Load the vocabulary with the same $vid from earlier.
  189. // This should return a vocabulary object since it now matches a real vid.
  190. $vocabulary = taxonomy_vocabulary_load($vid);
  191. $this->assertTrue(!empty($vocabulary) && is_object($vocabulary), 'Vocabulary is an object.');
  192. $this->assertEqual($vocabulary->vid, $vid, 'Valid vocabulary vid is the same as our previously invalid one.');
  193. }
  194. /**
  195. * Test deleting a taxonomy that contains terms.
  196. */
  197. function testTaxonomyVocabularyDeleteWithTerms() {
  198. // Delete any existing vocabularies.
  199. foreach (taxonomy_get_vocabularies() as $vocabulary) {
  200. taxonomy_vocabulary_delete($vocabulary->vid);
  201. }
  202. // Assert that there are no terms left.
  203. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
  204. // Create a new vocabulary and add a few terms to it.
  205. $vocabulary = $this->createVocabulary();
  206. $terms = array();
  207. for ($i = 0; $i < 5; $i++) {
  208. $terms[$i] = $this->createTerm($vocabulary);
  209. }
  210. // Set up hierarchy. term 2 is a child of 1 and 4 a child of 1 and 2.
  211. $terms[2]->parent = array($terms[1]->tid);
  212. taxonomy_term_save($terms[2]);
  213. $terms[4]->parent = array($terms[1]->tid, $terms[2]->tid);
  214. taxonomy_term_save($terms[4]);
  215. // Assert that there are now 5 terms.
  216. $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
  217. taxonomy_vocabulary_delete($vocabulary->vid);
  218. // Assert that there are no terms left.
  219. $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
  220. }
  221. /**
  222. * Ensure that the vocabulary static reset works correctly.
  223. */
  224. function testTaxonomyVocabularyLoadStaticReset() {
  225. $original_vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
  226. $this->assertTrue(is_object($original_vocabulary), 'Vocabulary loaded successfully.');
  227. $this->assertEqual($this->vocabulary->name, $original_vocabulary->name, 'Vocabulary loaded successfully.');
  228. // Change the name and description.
  229. $vocabulary = $original_vocabulary;
  230. $vocabulary->name = $this->randomName();
  231. $vocabulary->description = $this->randomName();
  232. taxonomy_vocabulary_save($vocabulary);
  233. // Load the vocabulary.
  234. $new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid);
  235. $this->assertEqual($new_vocabulary->name, $vocabulary->name);
  236. $this->assertEqual($new_vocabulary->name, $vocabulary->name);
  237. // Delete the vocabulary.
  238. taxonomy_vocabulary_delete($this->vocabulary->vid);
  239. $vocabularies = taxonomy_get_vocabularies();
  240. $this->assertTrue(!isset($vocabularies[$this->vocabulary->vid]), 'The vocabulary was deleted.');
  241. }
  242. /**
  243. * Tests for loading multiple vocabularies.
  244. */
  245. function testTaxonomyVocabularyLoadMultiple() {
  246. // Delete any existing vocabularies.
  247. foreach (taxonomy_get_vocabularies() as $vocabulary) {
  248. taxonomy_vocabulary_delete($vocabulary->vid);
  249. }
  250. // Create some vocabularies and assign weights.
  251. $vocabulary1 = $this->createVocabulary();
  252. $vocabulary1->weight = 0;
  253. taxonomy_vocabulary_save($vocabulary1);
  254. $vocabulary2 = $this->createVocabulary();
  255. $vocabulary2->weight = 1;
  256. taxonomy_vocabulary_save($vocabulary2);
  257. $vocabulary3 = $this->createVocabulary();
  258. $vocabulary3->weight = 2;
  259. taxonomy_vocabulary_save($vocabulary3);
  260. // Fetch the names for all vocabularies, confirm that they are keyed by
  261. // machine name.
  262. $names = taxonomy_vocabulary_get_names();
  263. $this->assertEqual($names[$vocabulary1->machine_name]->name, $vocabulary1->name, 'Vocabulary 1 name found.');
  264. // Fetch all of the vocabularies using taxonomy_get_vocabularies().
  265. // Confirm that the vocabularies are ordered by weight.
  266. $vocabularies = taxonomy_get_vocabularies();
  267. $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, 'Vocabulary was found in the vocabularies array.');
  268. $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, 'Vocabulary was found in the vocabularies array.');
  269. $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, 'Vocabulary was found in the vocabularies array.');
  270. // Fetch the vocabularies with taxonomy_vocabulary_load_multiple(), specifying IDs.
  271. // Ensure they are returned in the same order as the original array.
  272. $vocabularies = taxonomy_vocabulary_load_multiple(array($vocabulary3->vid, $vocabulary2->vid, $vocabulary1->vid));
  273. $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, 'Vocabulary loaded successfully by ID.');
  274. $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, 'Vocabulary loaded successfully by ID.');
  275. $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by ID.');
  276. // Fetch vocabulary 1 by name.
  277. $vocabulary = current(taxonomy_vocabulary_load_multiple(array(), array('name' => $vocabulary1->name)));
  278. $this->assertEqual($vocabulary->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by name.');
  279. // Fetch vocabulary 1 by name and ID.
  280. $this->assertEqual(current(taxonomy_vocabulary_load_multiple(array($vocabulary1->vid), array('name' => $vocabulary1->name)))->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by name and ID.');
  281. }
  282. /**
  283. * Tests that machine name changes are properly reflected.
  284. */
  285. function testTaxonomyVocabularyChangeMachineName() {
  286. // Add a field instance to the vocabulary.
  287. $field = array(
  288. 'field_name' => 'field_test',
  289. 'type' => 'test_field',
  290. );
  291. field_create_field($field);
  292. $instance = array(
  293. 'field_name' => 'field_test',
  294. 'entity_type' => 'taxonomy_term',
  295. 'bundle' => $this->vocabulary->machine_name,
  296. );
  297. field_create_instance($instance);
  298. // Change the machine name.
  299. $new_name = drupal_strtolower($this->randomName());
  300. $this->vocabulary->machine_name = $new_name;
  301. taxonomy_vocabulary_save($this->vocabulary);
  302. // Check that the field instance is still attached to the vocabulary.
  303. $this->assertTrue(field_info_instance('taxonomy_term', 'field_test', $new_name), 'The bundle name was updated correctly.');
  304. }
  305. /**
  306. * Test uninstall and reinstall of the taxonomy module.
  307. */
  308. function testUninstallReinstall() {
  309. // Fields and field instances attached to taxonomy term bundles should be
  310. // removed when the module is uninstalled.
  311. $this->field_name = drupal_strtolower($this->randomName() . '_field_name');
  312. $this->field = array('field_name' => $this->field_name, 'type' => 'text', 'cardinality' => 4);
  313. $this->field = field_create_field($this->field);
  314. $this->instance = array(
  315. 'field_name' => $this->field_name,
  316. 'entity_type' => 'taxonomy_term',
  317. 'bundle' => $this->vocabulary->machine_name,
  318. 'label' => $this->randomName() . '_label',
  319. );
  320. field_create_instance($this->instance);
  321. module_disable(array('taxonomy'));
  322. require_once DRUPAL_ROOT . '/includes/install.inc';
  323. drupal_uninstall_modules(array('taxonomy'));
  324. module_enable(array('taxonomy'));
  325. // Now create a vocabulary with the same name. All field instances
  326. // connected to this vocabulary name should have been removed when the
  327. // module was uninstalled. Creating a new field with the same name and
  328. // an instance of this field on the same bundle name should be successful.
  329. unset($this->vocabulary->vid);
  330. taxonomy_vocabulary_save($this->vocabulary);
  331. unset($this->field['id']);
  332. field_create_field($this->field);
  333. field_create_instance($this->instance);
  334. }
  335. }
  336. /**
  337. * Unit tests for taxonomy term functions.
  338. */
  339. class TaxonomyTermFunctionTestCase extends TaxonomyWebTestCase {
  340. public static function getInfo() {
  341. return array(
  342. 'name' => 'Taxonomy term unit tests',
  343. 'description' => 'Unit tests for taxonomy term functions.',
  344. 'group' => 'Taxonomy',
  345. );
  346. }
  347. function testTermDelete() {
  348. $vocabulary = $this->createVocabulary();
  349. $valid_term = $this->createTerm($vocabulary);
  350. // Delete a valid term.
  351. taxonomy_term_delete($valid_term->tid);
  352. $terms = taxonomy_term_load_multiple(array(), array('vid' => $vocabulary->vid));
  353. $this->assertTrue(empty($terms), 'Vocabulary is empty after deletion.');
  354. // Delete an invalid term. Should not throw any notices.
  355. taxonomy_term_delete(42);
  356. }
  357. /**
  358. * Test a taxonomy with terms that have multiple parents of different depths.
  359. */
  360. function testTaxonomyVocabularyTree() {
  361. // Create a new vocabulary with 6 terms.
  362. $vocabulary = $this->createVocabulary();
  363. $term = array();
  364. for ($i = 0; $i < 6; $i++) {
  365. $term[$i] = $this->createTerm($vocabulary);
  366. }
  367. // $term[2] is a child of 1 and 5.
  368. $term[2]->parent = array($term[1]->tid, $term[5]->tid);
  369. taxonomy_term_save($term[2]);
  370. // $term[3] is a child of 2.
  371. $term[3]->parent = array($term[2]->tid);
  372. taxonomy_term_save($term[3]);
  373. // $term[5] is a child of 4.
  374. $term[5]->parent = array($term[4]->tid);
  375. taxonomy_term_save($term[5]);
  376. /**
  377. * Expected tree:
  378. * term[0] | depth: 0
  379. * term[1] | depth: 0
  380. * -- term[2] | depth: 1
  381. * ---- term[3] | depth: 2
  382. * term[4] | depth: 0
  383. * -- term[5] | depth: 1
  384. * ---- term[2] | depth: 2
  385. * ------ term[3] | depth: 3
  386. */
  387. // Count $term[1] parents with $max_depth = 1.
  388. $tree = taxonomy_get_tree($vocabulary->vid, $term[1]->tid, 1);
  389. $this->assertEqual(1, count($tree), 'We have one parent with depth 1.');
  390. // Count all vocabulary tree elements.
  391. $tree = taxonomy_get_tree($vocabulary->vid);
  392. $this->assertEqual(8, count($tree), 'We have all vocabulary tree elements.');
  393. // Count elements in every tree depth.
  394. foreach ($tree as $element) {
  395. if (!isset($depth_count[$element->depth])) {
  396. $depth_count[$element->depth] = 0;
  397. }
  398. $depth_count[$element->depth]++;
  399. }
  400. $this->assertEqual(3, $depth_count[0], 'Three elements in taxonomy tree depth 0.');
  401. $this->assertEqual(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.');
  402. $this->assertEqual(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.');
  403. $this->assertEqual(1, $depth_count[3], 'One element in taxonomy tree depth 3.');
  404. }
  405. }
  406. /**
  407. * Test for legacy node bug.
  408. */
  409. class TaxonomyLegacyTestCase extends TaxonomyWebTestCase {
  410. public static function getInfo() {
  411. return array(
  412. 'name' => 'Test for legacy node bug.',
  413. 'description' => 'Posts an article with a taxonomy term and a date prior to 1970.',
  414. 'group' => 'Taxonomy',
  415. );
  416. }
  417. function setUp() {
  418. parent::setUp('taxonomy');
  419. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access'));
  420. $this->drupalLogin($this->admin_user);
  421. }
  422. /**
  423. * Test taxonomy functionality with nodes prior to 1970.
  424. */
  425. function testTaxonomyLegacyNode() {
  426. // Posts an article with a taxonomy term and a date prior to 1970.
  427. $langcode = LANGUAGE_NONE;
  428. $edit = array();
  429. $edit['title'] = $this->randomName();
  430. $edit['date'] = '1969-01-01 00:00:00 -0500';
  431. $edit["body[$langcode][0][value]"] = $this->randomName();
  432. $edit["field_tags[$langcode]"] = $this->randomName();
  433. $this->drupalPost('node/add/article', $edit, t('Save'));
  434. // Checks that the node has been saved.
  435. $node = $this->drupalGetNodeByTitle($edit['title']);
  436. $this->assertEqual($node->created, strtotime($edit['date']), 'Legacy node was saved with the right date.');
  437. }
  438. }
  439. /**
  440. * Tests for taxonomy term functions.
  441. */
  442. class TaxonomyTermTestCase extends TaxonomyWebTestCase {
  443. public static function getInfo() {
  444. return array(
  445. 'name' => 'Taxonomy term functions and forms.',
  446. 'description' => 'Test load, save and delete for taxonomy terms.',
  447. 'group' => 'Taxonomy',
  448. );
  449. }
  450. function setUp() {
  451. parent::setUp('taxonomy');
  452. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
  453. $this->drupalLogin($this->admin_user);
  454. $this->vocabulary = $this->createVocabulary();
  455. $field = array(
  456. 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
  457. 'type' => 'taxonomy_term_reference',
  458. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  459. 'settings' => array(
  460. 'allowed_values' => array(
  461. array(
  462. 'vocabulary' => $this->vocabulary->machine_name,
  463. 'parent' => 0,
  464. ),
  465. ),
  466. ),
  467. );
  468. field_create_field($field);
  469. $this->instance = array(
  470. 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
  471. 'bundle' => 'article',
  472. 'entity_type' => 'node',
  473. 'widget' => array(
  474. 'type' => 'options_select',
  475. ),
  476. 'display' => array(
  477. 'default' => array(
  478. 'type' => 'taxonomy_term_reference_link',
  479. ),
  480. ),
  481. );
  482. field_create_instance($this->instance);
  483. }
  484. /**
  485. * Test terms in a single and multiple hierarchy.
  486. */
  487. function testTaxonomyTermHierarchy() {
  488. // Create two taxonomy terms.
  489. $term1 = $this->createTerm($this->vocabulary);
  490. $term2 = $this->createTerm($this->vocabulary);
  491. // Check that hierarchy is flat.
  492. $vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
  493. $this->assertEqual(0, $vocabulary->hierarchy, 'Vocabulary is flat.');
  494. // Edit $term2, setting $term1 as parent.
  495. $edit = array();
  496. $edit['parent[]'] = array($term1->tid);
  497. $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));
  498. // Check the hierarchy.
  499. $children = taxonomy_get_children($term1->tid);
  500. $parents = taxonomy_get_parents($term2->tid);
  501. $this->assertTrue(isset($children[$term2->tid]), 'Child found correctly.');
  502. $this->assertTrue(isset($parents[$term1->tid]), 'Parent found correctly.');
  503. // Load and save a term, confirming that parents are still set.
  504. $term = taxonomy_term_load($term2->tid);
  505. taxonomy_term_save($term);
  506. $parents = taxonomy_get_parents($term2->tid);
  507. $this->assertTrue(isset($parents[$term1->tid]), 'Parent found correctly.');
  508. // Create a third term and save this as a parent of term2.
  509. $term3 = $this->createTerm($this->vocabulary);
  510. $term2->parent = array($term1->tid, $term3->tid);
  511. taxonomy_term_save($term2);
  512. $parents = taxonomy_get_parents($term2->tid);
  513. $this->assertTrue(isset($parents[$term1->tid]) && isset($parents[$term3->tid]), 'Both parents found successfully.');
  514. }
  515. /**
  516. * Test that hook_node_$op implementations work correctly.
  517. *
  518. * Save & edit a node and assert that taxonomy terms are saved/loaded properly.
  519. */
  520. function testTaxonomyNode() {
  521. // Create two taxonomy terms.
  522. $term1 = $this->createTerm($this->vocabulary);
  523. $term2 = $this->createTerm($this->vocabulary);
  524. // Post an article.
  525. $edit = array();
  526. $langcode = LANGUAGE_NONE;
  527. $edit["title"] = $this->randomName();
  528. $edit["body[$langcode][0][value]"] = $this->randomName();
  529. $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid;
  530. $this->drupalPost('node/add/article', $edit, t('Save'));
  531. // Check that the term is displayed when the node is viewed.
  532. $node = $this->drupalGetNodeByTitle($edit["title"]);
  533. $this->drupalGet('node/' . $node->nid);
  534. $this->assertText($term1->name, 'Term is displayed when viewing the node.');
  535. // Edit the node with a different term.
  536. $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term2->tid;
  537. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  538. $this->drupalGet('node/' . $node->nid);
  539. $this->assertText($term2->name, 'Term is displayed when viewing the node.');
  540. // Preview the node.
  541. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Preview'));
  542. $this->assertNoUniqueText($term2->name, 'Term is displayed when previewing the node.');
  543. $this->drupalPost(NULL, NULL, t('Preview'));
  544. $this->assertNoUniqueText($term2->name, 'Term is displayed when previewing the node again.');
  545. }
  546. /**
  547. * Test term creation with a free-tagging vocabulary from the node form.
  548. */
  549. function testNodeTermCreationAndDeletion() {
  550. // Enable tags in the vocabulary.
  551. $instance = $this->instance;
  552. $instance['widget'] = array('type' => 'taxonomy_autocomplete');
  553. $instance['bundle'] = 'page';
  554. field_create_instance($instance);
  555. $terms = array(
  556. 'term1' => $this->randomName(),
  557. 'term2' => $this->randomName() . ', ' . $this->randomName(),
  558. 'term3' => $this->randomName(),
  559. );
  560. $edit = array();
  561. $langcode = LANGUAGE_NONE;
  562. $edit["title"] = $this->randomName();
  563. $edit["body[$langcode][0][value]"] = $this->randomName();
  564. // Insert the terms in a comma separated list. Vocabulary 1 is a
  565. // free-tagging field created by the default profile.
  566. $edit[$instance['field_name'] . "[$langcode]"] = drupal_implode_tags($terms);
  567. // Preview and verify the terms appear but are not created.
  568. $this->drupalPost('node/add/page', $edit, t('Preview'));
  569. foreach ($terms as $term) {
  570. $this->assertText($term, 'The term appears on the node preview.');
  571. }
  572. $tree = taxonomy_get_tree($this->vocabulary->vid);
  573. $this->assertTrue(empty($tree), 'The terms are not created on preview.');
  574. // taxonomy.module does not maintain its static caches.
  575. drupal_static_reset();
  576. // Save, creating the terms.
  577. $this->drupalPost('node/add/page', $edit, t('Save'));
  578. $this->assertRaw(t('@type %title has been created.', array('@type' => t('Basic page'), '%title' => $edit["title"])), 'The node was created successfully.');
  579. foreach ($terms as $term) {
  580. $this->assertText($term, 'The term was saved and appears on the node page.');
  581. }
  582. // Get the created terms.
  583. $term_objects = array();
  584. foreach ($terms as $key => $term) {
  585. $term_objects[$key] = taxonomy_get_term_by_name($term);
  586. $term_objects[$key] = reset($term_objects[$key]);
  587. }
  588. // Delete term 1.
  589. $this->drupalPost('taxonomy/term/' . $term_objects['term1']->tid . '/edit', array(), t('Delete'));
  590. $this->drupalPost(NULL, NULL, t('Delete'));
  591. $term_names = array($term_objects['term2']->name, $term_objects['term3']->name);
  592. // Get the node.
  593. $node = $this->drupalGetNodeByTitle($edit["title"]);
  594. $this->drupalGet('node/' . $node->nid);
  595. foreach ($term_names as $term_name) {
  596. $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)));
  597. }
  598. $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)));
  599. // Test autocomplete on term 2, which contains a comma.
  600. // The term will be quoted, and the " will be encoded in unicode (\u0022).
  601. $input = substr($term_objects['term2']->name, 0, 3);
  602. $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
  603. $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)));
  604. // Test autocomplete on term 3 - it is alphanumeric only, so no extra
  605. // quoting.
  606. $input = substr($term_objects['term3']->name, 0, 3);
  607. $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input);
  608. $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)));
  609. // Test taxonomy autocomplete with a nonexistent field.
  610. $field_name = $this->randomName();
  611. $tag = $this->randomName();
  612. $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name));
  613. $this->assertFalse(field_info_field($field_name), format_string('Field %field_name does not exist.', array('%field_name' => $field_name)));
  614. $this->drupalGet('taxonomy/autocomplete/' . $field_name . '/' . $tag);
  615. $this->assertRaw($message, 'Autocomplete returns correct error message when the taxonomy field does not exist.');
  616. }
  617. /**
  618. * Tests term autocompletion edge cases with slashes in the names.
  619. */
  620. function testTermAutocompletion() {
  621. // Add a term with a slash in the name.
  622. $first_term = $this->createTerm($this->vocabulary);
  623. $first_term->name = '10/16/2011';
  624. taxonomy_term_save($first_term);
  625. // Add another term that differs after the slash character.
  626. $second_term = $this->createTerm($this->vocabulary);
  627. $second_term->name = '10/17/2011';
  628. taxonomy_term_save($second_term);
  629. // Add another term that has both a comma and a slash character.
  630. $third_term = $this->createTerm($this->vocabulary);
  631. $third_term->name = 'term with, a comma and / a slash';
  632. taxonomy_term_save($third_term);
  633. // Try to autocomplete a term name that matches both terms.
  634. // We should get both term in a json encoded string.
  635. $input = '10/';
  636. $path = 'taxonomy/autocomplete/taxonomy_';
  637. $path .= $this->vocabulary->machine_name . '/' . $input;
  638. // The result order is not guaranteed, so check each term separately.
  639. $url = url($path, array('absolute' => TRUE));
  640. $result = drupal_http_request($url);
  641. $data = drupal_json_decode($result->data);
  642. $this->assertEqual($data[$first_term->name], check_plain($first_term->name), 'Autocomplete returned the first matching term.');
  643. $this->assertEqual($data[$second_term->name], check_plain($second_term->name), 'Autocomplete returned the second matching term.');
  644. // Try to autocomplete a term name that matches first term.
  645. // We should only get the first term in a json encoded string.
  646. $input = '10/16';
  647. $url = 'taxonomy/autocomplete/taxonomy_';
  648. $url .= $this->vocabulary->machine_name . '/' . $input;
  649. $this->drupalGet($url);
  650. $target = array($first_term->name => check_plain($first_term->name));
  651. $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns only the expected matching term.');
  652. // Try to autocomplete a term name with both a comma and a slash.
  653. $input = '"term with, comma and / a';
  654. $url = 'taxonomy/autocomplete/taxonomy_';
  655. $url .= $this->vocabulary->machine_name . '/' . $input;
  656. $this->drupalGet($url);
  657. $n = $third_term->name;
  658. // Term names containing commas or quotes must be wrapped in quotes.
  659. if (strpos($third_term->name, ',') !== FALSE || strpos($third_term->name, '"') !== FALSE) {
  660. $n = '"' . str_replace('"', '""', $third_term->name) . '"';
  661. }
  662. $target = array($n => check_plain($third_term->name));
  663. $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns a term containing a comma and a slash.');
  664. }
  665. /**
  666. * Save, edit and delete a term using the user interface.
  667. */
  668. function testTermInterface() {
  669. $edit = array(
  670. 'name' => $this->randomName(12),
  671. 'description[value]' => $this->randomName(100),
  672. );
  673. // Explicitly set the parents field to 'root', to ensure that
  674. // taxonomy_form_term_submit() handles the invalid term ID correctly.
  675. $edit['parent[]'] = array(0);
  676. // Create the term to edit.
  677. $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
  678. $terms = taxonomy_get_term_by_name($edit['name']);
  679. $term = reset($terms);
  680. $this->assertNotNull($term, 'Term found in database.');
  681. // Submitting a term takes us to the add page; we need the List page.
  682. $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
  683. // Test edit link as accessed from Taxonomy administration pages.
  684. // Because Simpletest creates its own database when running tests, we know
  685. // the first edit link found on the listing page is to our term.
  686. $this->clickLink(t('edit'));
  687. $this->assertRaw($edit['name'], 'The randomly generated term name is present.');
  688. $this->assertText($edit['description[value]'], 'The randomly generated term description is present.');
  689. $edit = array(
  690. 'name' => $this->randomName(14),
  691. 'description[value]' => $this->randomName(102),
  692. );
  693. // Edit the term.
  694. $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
  695. // Check that the term is still present at admin UI after edit.
  696. $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
  697. $this->assertText($edit['name'], 'The randomly generated term name is present.');
  698. $this->assertLink(t('edit'));
  699. // View the term and check that it is correct.
  700. $this->drupalGet('taxonomy/term/' . $term->tid);
  701. $this->assertText($edit['name'], 'The randomly generated term name is present.');
  702. $this->assertText($edit['description[value]'], 'The randomly generated term description is present.');
  703. // Did this page request display a 'term-listing-heading'?
  704. $this->assertPattern('|class="taxonomy-term-description"|', 'Term page displayed the term description element.');
  705. // Check that it does NOT show a description when description is blank.
  706. $term->description = '';
  707. taxonomy_term_save($term);
  708. $this->drupalGet('taxonomy/term/' . $term->tid);
  709. $this->assertNoPattern('|class="taxonomy-term-description"|', 'Term page did not display the term description when description was blank.');
  710. // Check that the term feed page is working.
  711. $this->drupalGet('taxonomy/term/' . $term->tid . '/feed');
  712. // Check that the term edit page does not try to interpret additional path
  713. // components as arguments for taxonomy_form_term().
  714. $this->drupalGet('taxonomy/term/' . $term->tid . '/edit/' . $this->randomName());
  715. // Delete the term.
  716. $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', array(), t('Delete'));
  717. $this->drupalPost(NULL, NULL, t('Delete'));
  718. // Assert that the term no longer exists.
  719. $this->drupalGet('taxonomy/term/' . $term->tid);
  720. $this->assertResponse(404, 'The taxonomy term page was not found.');
  721. }
  722. /**
  723. * Save, edit and delete a term using the user interface.
  724. */
  725. function testTermReorder() {
  726. $this->createTerm($this->vocabulary);
  727. $this->createTerm($this->vocabulary);
  728. $this->createTerm($this->vocabulary);
  729. // Fetch the created terms in the default alphabetical order, i.e. term1
  730. // precedes term2 alphabetically, and term2 precedes term3.
  731. drupal_static_reset('taxonomy_get_tree');
  732. drupal_static_reset('taxonomy_get_treeparent');
  733. drupal_static_reset('taxonomy_get_treeterms');
  734. list($term1, $term2, $term3) = taxonomy_get_tree($this->vocabulary->vid);
  735. $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
  736. // Each term has four hidden fields, "tid:1:0[tid]", "tid:1:0[parent]",
  737. // "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2,
  738. // term3, term1 by setting weight property, make term3 a child of term2 by
  739. // setting the parent and depth properties, and update all hidden fields.
  740. $edit = array(
  741. 'tid:' . $term2->tid . ':0[tid]' => $term2->tid,
  742. 'tid:' . $term2->tid . ':0[parent]' => 0,
  743. 'tid:' . $term2->tid . ':0[depth]' => 0,
  744. 'tid:' . $term2->tid . ':0[weight]' => 0,
  745. 'tid:' . $term3->tid . ':0[tid]' => $term3->tid,
  746. 'tid:' . $term3->tid . ':0[parent]' => $term2->tid,
  747. 'tid:' . $term3->tid . ':0[depth]' => 1,
  748. 'tid:' . $term3->tid . ':0[weight]' => 1,
  749. 'tid:' . $term1->tid . ':0[tid]' => $term1->tid,
  750. 'tid:' . $term1->tid . ':0[parent]' => 0,
  751. 'tid:' . $term1->tid . ':0[depth]' => 0,
  752. 'tid:' . $term1->tid . ':0[weight]' => 2,
  753. );
  754. $this->drupalPost(NULL, $edit, t('Save'));
  755. drupal_static_reset('taxonomy_get_tree');
  756. drupal_static_reset('taxonomy_get_treeparent');
  757. drupal_static_reset('taxonomy_get_treeterms');
  758. $terms = taxonomy_get_tree($this->vocabulary->vid);
  759. $this->assertEqual($terms[0]->tid, $term2->tid, 'Term 2 was moved above term 1.');
  760. $this->assertEqual($terms[1]->parents, array($term2->tid), 'Term 3 was made a child of term 2.');
  761. $this->assertEqual($terms[2]->tid, $term1->tid, 'Term 1 was moved below term 2.');
  762. $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name, array(), t('Reset to alphabetical'));
  763. // Submit confirmation form.
  764. $this->drupalPost(NULL, array(), t('Reset to alphabetical'));
  765. drupal_static_reset('taxonomy_get_tree');
  766. drupal_static_reset('taxonomy_get_treeparent');
  767. drupal_static_reset('taxonomy_get_treeterms');
  768. $terms = taxonomy_get_tree($this->vocabulary->vid);
  769. $this->assertEqual($terms[0]->tid, $term1->tid, 'Term 1 was moved to back above term 2.');
  770. $this->assertEqual($terms[1]->tid, $term2->tid, 'Term 2 was moved to back below term 1.');
  771. $this->assertEqual($terms[2]->tid, $term3->tid, 'Term 3 is still below term 2.');
  772. $this->assertEqual($terms[2]->parents, array($term2->tid), 'Term 3 is still a child of term 2.' . var_export($terms[1]->tid, 1));
  773. }
  774. /**
  775. * Test saving a term with multiple parents through the UI.
  776. */
  777. function testTermMultipleParentsInterface() {
  778. // Add a new term to the vocabulary so that we can have multiple parents.
  779. $parent = $this->createTerm($this->vocabulary);
  780. // Add a new term with multiple parents.
  781. $edit = array(
  782. 'name' => $this->randomName(12),
  783. 'description[value]' => $this->randomName(100),
  784. 'parent[]' => array(0, $parent->tid),
  785. );
  786. // Save the new term.
  787. $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
  788. // Check that the term was successfully created.
  789. $terms = taxonomy_get_term_by_name($edit['name']);
  790. $term = reset($terms);
  791. $this->assertNotNull($term, 'Term found in database.');
  792. $this->assertEqual($edit['name'], $term->name, 'Term name was successfully saved.');
  793. $this->assertEqual($edit['description[value]'], $term->description, 'Term description was successfully saved.');
  794. // Check that the parent tid is still there. The other parent (<root>) is
  795. // not added by taxonomy_get_parents().
  796. $parents = taxonomy_get_parents($term->tid);
  797. $parent = reset($parents);
  798. $this->assertEqual($edit['parent[]'][1], $parent->tid, 'Term parents were successfully saved.');
  799. }
  800. /**
  801. * Test taxonomy_get_term_by_name().
  802. */
  803. function testTaxonomyGetTermByName() {
  804. $term = $this->createTerm($this->vocabulary);
  805. // Load the term with the exact name.
  806. $terms = taxonomy_get_term_by_name($term->name);
  807. $this->assertTrue(isset($terms[$term->tid]), 'Term loaded using exact name.');
  808. // Load the term with space concatenated.
  809. $terms = taxonomy_get_term_by_name(' ' . $term->name . ' ');
  810. $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with extra whitespace.');
  811. // Load the term with name uppercased.
  812. $terms = taxonomy_get_term_by_name(strtoupper($term->name));
  813. $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with uppercased name.');
  814. // Load the term with name lowercased.
  815. $terms = taxonomy_get_term_by_name(strtolower($term->name));
  816. $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with lowercased name.');
  817. // Try to load an invalid term name.
  818. $terms = taxonomy_get_term_by_name('Banana');
  819. $this->assertFalse($terms);
  820. // Try to load the term using a substring of the name.
  821. $terms = taxonomy_get_term_by_name(drupal_substr($term->name, 2));
  822. $this->assertFalse($terms);
  823. // Create a new term in a different vocabulary with the same name.
  824. $new_vocabulary = $this->createVocabulary();
  825. $new_term = new stdClass();
  826. $new_term->name = $term->name;
  827. $new_term->vid = $new_vocabulary->vid;
  828. taxonomy_term_save($new_term);
  829. // Load multiple terms with the same name.
  830. $terms = taxonomy_get_term_by_name($term->name);
  831. $this->assertEqual(count($terms), 2, 'Two terms loaded with the same name.');
  832. // Load single term when restricted to one vocabulary.
  833. $terms = taxonomy_get_term_by_name($term->name, $this->vocabulary->machine_name);
  834. $this->assertEqual(count($terms), 1, 'One term loaded when restricted by vocabulary.');
  835. $this->assertTrue(isset($terms[$term->tid]), 'Term loaded using exact name and vocabulary machine name.');
  836. // Create a new term with another name.
  837. $term2 = $this->createTerm($this->vocabulary);
  838. // Try to load a term by name that doesn't exist in this vocabulary but
  839. // exists in another vocabulary.
  840. $terms = taxonomy_get_term_by_name($term2->name, $new_vocabulary->machine_name);
  841. $this->assertFalse($terms, 'Invalid term name restricted by vocabulary machine name not loaded.');
  842. // Try to load terms filtering by a non-existing vocabulary.
  843. $terms = taxonomy_get_term_by_name($term2->name, 'non_existing_vocabulary');
  844. $this->assertEqual(count($terms), 0, 'No terms loaded when restricted by a non-existing vocabulary.');
  845. }
  846. }
  847. /**
  848. * Tests the rendering of term reference fields in RSS feeds.
  849. */
  850. class TaxonomyRSSTestCase extends TaxonomyWebTestCase {
  851. public static function getInfo() {
  852. return array(
  853. 'name' => 'Taxonomy RSS Content.',
  854. 'description' => 'Ensure that data added as terms appears in RSS feeds if "RSS Category" format is selected.',
  855. 'group' => 'Taxonomy',
  856. );
  857. }
  858. function setUp() {
  859. parent::setUp('taxonomy');
  860. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access', 'administer content types'));
  861. $this->drupalLogin($this->admin_user);
  862. $this->vocabulary = $this->createVocabulary();
  863. $field = array(
  864. 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
  865. 'type' => 'taxonomy_term_reference',
  866. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  867. 'settings' => array(
  868. 'allowed_values' => array(
  869. array(
  870. 'vocabulary' => $this->vocabulary->machine_name,
  871. 'parent' => 0,
  872. ),
  873. ),
  874. ),
  875. );
  876. field_create_field($field);
  877. $this->instance = array(
  878. 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
  879. 'bundle' => 'article',
  880. 'entity_type' => 'node',
  881. 'widget' => array(
  882. 'type' => 'options_select',
  883. ),
  884. 'display' => array(
  885. 'default' => array(
  886. 'type' => 'taxonomy_term_reference_link',
  887. ),
  888. ),
  889. );
  890. field_create_instance($this->instance);
  891. }
  892. /**
  893. * Tests that terms added to nodes are displayed in core RSS feed.
  894. *
  895. * Create a node and assert that taxonomy terms appear in rss.xml.
  896. */
  897. function testTaxonomyRSS() {
  898. // Create two taxonomy terms.
  899. $term1 = $this->createTerm($this->vocabulary);
  900. // RSS display must be added manually.
  901. $this->drupalGet("admin/structure/types/manage/article/display");
  902. $edit = array(
  903. "view_modes_custom[rss]" => '1',
  904. );
  905. $this->drupalPost(NULL, $edit, t('Save'));
  906. // Change the format to 'RSS category'.
  907. $this->drupalGet("admin/structure/types/manage/article/display/rss");
  908. $edit = array(
  909. "fields[taxonomy_" . $this->vocabulary->machine_name . "][type]" => 'taxonomy_term_reference_rss_category',
  910. );
  911. $this->drupalPost(NULL, $edit, t('Save'));
  912. // Post an article.
  913. $edit = array();
  914. $langcode = LANGUAGE_NONE;
  915. $edit["title"] = $this->randomName();
  916. $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid;
  917. $this->drupalPost('node/add/article', $edit, t('Save'));
  918. // Check that the term is displayed when the RSS feed is viewed.
  919. $this->drupalGet('rss.xml');
  920. $test_element = array(
  921. 'key' => 'category',
  922. 'value' => $term1->name,
  923. 'attributes' => array(
  924. 'domain' => url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE)),
  925. ),
  926. );
  927. $this->assertRaw(format_xml_elements(array($test_element)), 'Term is displayed when viewing the rss feed.');
  928. }
  929. }
  930. /**
  931. * Tests the hook implementations that maintain the taxonomy index.
  932. */
  933. class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase {
  934. public static function getInfo() {
  935. return array(
  936. 'name' => 'Taxonomy term index',
  937. 'description' => 'Tests the hook implementations that maintain the taxonomy index.',
  938. 'group' => 'Taxonomy',
  939. );
  940. }
  941. function setUp() {
  942. parent::setUp('taxonomy');
  943. // Create an administrative user.
  944. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
  945. $this->drupalLogin($this->admin_user);
  946. // Create a vocabulary and add two term reference fields to article nodes.
  947. $this->vocabulary = $this->createVocabulary();
  948. $this->field_name_1 = drupal_strtolower($this->randomName());
  949. $this->field_1 = array(
  950. 'field_name' => $this->field_name_1,
  951. 'type' => 'taxonomy_term_reference',
  952. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  953. 'settings' => array(
  954. 'allowed_values' => array(
  955. array(
  956. 'vocabulary' => $this->vocabulary->machine_name,
  957. 'parent' => 0,
  958. ),
  959. ),
  960. ),
  961. );
  962. field_create_field($this->field_1);
  963. $this->instance_1 = array(
  964. 'field_name' => $this->field_name_1,
  965. 'bundle' => 'article',
  966. 'entity_type' => 'node',
  967. 'widget' => array(
  968. 'type' => 'options_select',
  969. ),
  970. 'display' => array(
  971. 'default' => array(
  972. 'type' => 'taxonomy_term_reference_link',
  973. ),
  974. ),
  975. );
  976. field_create_instance($this->instance_1);
  977. $this->field_name_2 = drupal_strtolower($this->randomName());
  978. $this->field_2 = array(
  979. 'field_name' => $this->field_name_2,
  980. 'type' => 'taxonomy_term_reference',
  981. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  982. 'settings' => array(
  983. 'allowed_values' => array(
  984. array(
  985. 'vocabulary' => $this->vocabulary->machine_name,
  986. 'parent' => 0,
  987. ),
  988. ),
  989. ),
  990. );
  991. field_create_field($this->field_2);
  992. $this->instance_2 = array(
  993. 'field_name' => $this->field_name_2,
  994. 'bundle' => 'article',
  995. 'entity_type' => 'node',
  996. 'widget' => array(
  997. 'type' => 'options_select',
  998. ),
  999. 'display' => array(
  1000. 'default' => array(
  1001. 'type' => 'taxonomy_term_reference_link',
  1002. ),
  1003. ),
  1004. );
  1005. field_create_instance($this->instance_2);
  1006. }
  1007. /**
  1008. * Tests that the taxonomy index is maintained properly.
  1009. */
  1010. function testTaxonomyIndex() {
  1011. // Create terms in the vocabulary.
  1012. $term_1 = $this->createTerm($this->vocabulary);
  1013. $term_2 = $this->createTerm($this->vocabulary);
  1014. // Post an article.
  1015. $edit = array();
  1016. $langcode = LANGUAGE_NONE;
  1017. $edit["title"] = $this->randomName();
  1018. $edit["body[$langcode][0][value]"] = $this->randomName();
  1019. $edit["{$this->field_name_1}[$langcode][]"] = $term_1->tid;
  1020. $edit["{$this->field_name_2}[$langcode][]"] = $term_1->tid;
  1021. $this->drupalPost('node/add/article', $edit, t('Save'));
  1022. // Check that the term is indexed, and only once.
  1023. $node = $this->drupalGetNodeByTitle($edit["title"]);
  1024. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1025. ':nid' => $node->nid,
  1026. ':tid' => $term_1->tid,
  1027. ))->fetchField();
  1028. $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
  1029. // Update the article to change one term.
  1030. $edit["{$this->field_name_1}[$langcode][]"] = $term_2->tid;
  1031. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  1032. // Check that both terms are indexed.
  1033. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1034. ':nid' => $node->nid,
  1035. ':tid' => $term_1->tid,
  1036. ))->fetchField();
  1037. $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
  1038. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1039. ':nid' => $node->nid,
  1040. ':tid' => $term_2->tid,
  1041. ))->fetchField();
  1042. $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
  1043. // Update the article to change another term.
  1044. $edit["{$this->field_name_2}[$langcode][]"] = $term_2->tid;
  1045. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  1046. // Check that only one term is indexed.
  1047. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1048. ':nid' => $node->nid,
  1049. ':tid' => $term_1->tid,
  1050. ))->fetchField();
  1051. $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
  1052. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1053. ':nid' => $node->nid,
  1054. ':tid' => $term_2->tid,
  1055. ))->fetchField();
  1056. $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
  1057. // Redo the above tests without interface.
  1058. $update_node = array(
  1059. 'nid' => $node->nid,
  1060. 'vid' => $node->vid,
  1061. 'uid' => $node->uid,
  1062. 'type' => $node->type,
  1063. 'title' => $this->randomName(),
  1064. );
  1065. // Update the article with no term changed.
  1066. $updated_node = (object) $update_node;
  1067. node_save($updated_node);
  1068. // Check that the index was not changed.
  1069. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1070. ':nid' => $node->nid,
  1071. ':tid' => $term_1->tid,
  1072. ))->fetchField();
  1073. $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
  1074. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1075. ':nid' => $node->nid,
  1076. ':tid' => $term_2->tid,
  1077. ))->fetchField();
  1078. $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
  1079. // Update the article to change one term.
  1080. $update_node[$this->field_name_1][$langcode] = array(array('tid' => $term_1->tid));
  1081. $updated_node = (object) $update_node;
  1082. node_save($updated_node);
  1083. // Check that both terms are indexed.
  1084. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1085. ':nid' => $node->nid,
  1086. ':tid' => $term_1->tid,
  1087. ))->fetchField();
  1088. $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
  1089. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1090. ':nid' => $node->nid,
  1091. ':tid' => $term_2->tid,
  1092. ))->fetchField();
  1093. $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
  1094. // Update the article to change another term.
  1095. $update_node[$this->field_name_2][$langcode] = array(array('tid' => $term_1->tid));
  1096. $updated_node = (object) $update_node;
  1097. node_save($updated_node);
  1098. // Check that only one term is indexed.
  1099. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1100. ':nid' => $node->nid,
  1101. ':tid' => $term_1->tid,
  1102. ))->fetchField();
  1103. $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
  1104. $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array(
  1105. ':nid' => $node->nid,
  1106. ':tid' => $term_2->tid,
  1107. ))->fetchField();
  1108. $this->assertEqual(0, $index_count, 'Term 2 is not indexed.');
  1109. }
  1110. /**
  1111. * Tests that there is a link to the parent term on the child term page.
  1112. */
  1113. function testTaxonomyTermHierarchyBreadcrumbs() {
  1114. // Create two taxonomy terms and set term2 as the parent of term1.
  1115. $term1 = $this->createTerm($this->vocabulary);
  1116. $term2 = $this->createTerm($this->vocabulary);
  1117. $term1->parent = array($term2->tid);
  1118. taxonomy_term_save($term1);
  1119. // Verify that the page breadcrumbs include a link to the parent term.
  1120. $this->drupalGet('taxonomy/term/' . $term1->tid);
  1121. $this->assertRaw(l($term2->name, 'taxonomy/term/' . $term2->tid), 'Parent term link is displayed when viewing the node.');
  1122. }
  1123. }
  1124. /**
  1125. * Test the taxonomy_term_load_multiple() function.
  1126. */
  1127. class TaxonomyLoadMultipleTestCase extends TaxonomyWebTestCase {
  1128. public static function getInfo() {
  1129. return array(
  1130. 'name' => 'Taxonomy term multiple loading',
  1131. 'description' => 'Test the loading of multiple taxonomy terms at once',
  1132. 'group' => 'Taxonomy',
  1133. );
  1134. }
  1135. function setUp() {
  1136. parent::setUp();
  1137. $this->taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
  1138. $this->drupalLogin($this->taxonomy_admin);
  1139. }
  1140. /**
  1141. * Create a vocabulary and some taxonomy terms, ensuring they're loaded
  1142. * correctly using taxonomy_term_load_multiple().
  1143. */
  1144. function testTaxonomyTermMultipleLoad() {
  1145. // Create a vocabulary.
  1146. $vocabulary = $this->createVocabulary();
  1147. // Create five terms in the vocabulary.
  1148. $i = 0;
  1149. while ($i < 5) {
  1150. $i++;
  1151. $this->createTerm($vocabulary);
  1152. }
  1153. // Load the terms from the vocabulary.
  1154. $terms = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
  1155. $count = count($terms);
  1156. $this->assertEqual($count, 5, format_string('Correct number of terms were loaded. !count terms.', array('!count' => $count)));
  1157. // Load the same terms again by tid.
  1158. $terms2 = taxonomy_term_load_multiple(array_keys($terms));
  1159. $this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.');
  1160. $this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.');
  1161. // Load the terms by tid, with a condition on vid.
  1162. $terms3 = taxonomy_term_load_multiple(array_keys($terms2), array('vid' => $vocabulary->vid));
  1163. $this->assertEqual($terms2, $terms3);
  1164. // Remove one term from the array, then delete it.
  1165. $deleted = array_shift($terms3);
  1166. taxonomy_term_delete($deleted->tid);
  1167. $deleted_term = taxonomy_term_load($deleted->tid);
  1168. $this->assertFalse($deleted_term);
  1169. // Load terms from the vocabulary by vid.
  1170. $terms4 = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
  1171. $this->assertEqual(count($terms4), 4, 'Correct number of terms were loaded.');
  1172. $this->assertFalse(isset($terms4[$deleted->tid]));
  1173. // Create a single term and load it by name.
  1174. $term = $this->createTerm($vocabulary);
  1175. $loaded_terms = taxonomy_term_load_multiple(array(), array('name' => $term->name));
  1176. $this->assertEqual(count($loaded_terms), 1, 'One term was loaded.');
  1177. $loaded_term = reset($loaded_terms);
  1178. $this->assertEqual($term->tid, $loaded_term->tid, 'Term loaded by name successfully.');
  1179. }
  1180. }
  1181. /**
  1182. * Tests for taxonomy hook invocation.
  1183. */
  1184. class TaxonomyHooksTestCase extends TaxonomyWebTestCase {
  1185. public static function getInfo() {
  1186. return array(
  1187. 'name' => 'Taxonomy term hooks',
  1188. 'description' => 'Hooks for taxonomy term load/save/delete.',
  1189. 'group' => 'Taxonomy',
  1190. );
  1191. }
  1192. function setUp() {
  1193. parent::setUp('taxonomy', 'taxonomy_test');
  1194. $taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
  1195. $this->drupalLogin($taxonomy_admin);
  1196. }
  1197. /**
  1198. * Test that hooks are run correctly on creating, editing and deleting a term.
  1199. */
  1200. function testTaxonomyTermHooks() {
  1201. $vocabulary = $this->createVocabulary();
  1202. // Create a term with one antonym.
  1203. $edit = array(
  1204. 'name' => $this->randomName(),
  1205. 'antonym' => 'Long',
  1206. );
  1207. $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save'));
  1208. $terms = taxonomy_get_term_by_name($edit['name']);
  1209. $term = reset($terms);
  1210. $this->assertEqual($term->antonym, $edit['antonym'], 'Antonym was loaded into the term object.');
  1211. // Update the term with a different antonym.
  1212. $edit = array(
  1213. 'name' => $this->randomName(),
  1214. 'antonym' => 'Short',
  1215. );
  1216. $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
  1217. taxonomy_terms_static_reset();
  1218. $term = taxonomy_term_load($term->tid);
  1219. $this->assertEqual($edit['antonym'], $term->antonym, 'Antonym was successfully edited.');
  1220. // Delete the term.
  1221. taxonomy_term_delete($term->tid);
  1222. $antonym = db_query('SELECT tid FROM {taxonomy_term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
  1223. $this->assertFalse($antonym, 'The antonym were deleted from the database.');
  1224. }
  1225. }
  1226. /**
  1227. * Tests for taxonomy term field and formatter.
  1228. */
  1229. class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase {
  1230. protected $instance;
  1231. protected $vocabulary;
  1232. public static function getInfo() {
  1233. return array(
  1234. 'name' => 'Taxonomy term reference field',
  1235. 'description' => 'Test the creation of term fields.',
  1236. 'group' => 'Taxonomy',
  1237. );
  1238. }
  1239. function setUp() {
  1240. parent::setUp('field_test');
  1241. $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy'));
  1242. $this->drupalLogin($web_user);
  1243. $this->vocabulary = $this->createVocabulary();
  1244. // Setup a field and instance.
  1245. $this->field_name = drupal_strtolower($this->randomName());
  1246. $this->field = array(
  1247. 'field_name' => $this->field_name,
  1248. 'type' => 'taxonomy_term_reference',
  1249. 'settings' => array(
  1250. 'allowed_values' => array(
  1251. array(
  1252. 'vocabulary' => $this->vocabulary->machine_name,
  1253. 'parent' => '0',
  1254. ),
  1255. ),
  1256. )
  1257. );
  1258. field_create_field($this->field);
  1259. $this->instance = array(
  1260. 'field_name' => $this->field_name,
  1261. 'entity_type' => 'test_entity',
  1262. 'bundle' => 'test_bundle',
  1263. 'widget' => array(
  1264. 'type' => 'options_select',
  1265. ),
  1266. 'display' => array(
  1267. 'full' => array(
  1268. 'type' => 'taxonomy_term_reference_link',
  1269. ),
  1270. ),
  1271. );
  1272. field_create_instance($this->instance);
  1273. }
  1274. /**
  1275. * Test term field validation.
  1276. */
  1277. function testTaxonomyTermFieldValidation() {
  1278. // Test valid and invalid values with field_attach_validate().
  1279. $langcode = LANGUAGE_NONE;
  1280. $entity = field_test_create_stub_entity();
  1281. $term = $this->createTerm($this->vocabulary);
  1282. $entity->{$this->field_name}[$langcode][0]['tid'] = $term->tid;
  1283. try {
  1284. field_attach_validate('test_entity', $entity);
  1285. $this->pass('Correct term does not cause validation error.');
  1286. }
  1287. catch (FieldValidationException $e) {
  1288. $this->fail('Correct term does not cause validation error.');
  1289. }
  1290. $entity = field_test_create_stub_entity();
  1291. $bad_term = $this->createTerm($this->createVocabulary());
  1292. $entity->{$this->field_name}[$langcode][0]['tid'] = $bad_term->tid;
  1293. try {
  1294. field_attach_validate('test_entity', $entity);
  1295. $this->fail('Wrong term causes validation error.');
  1296. }
  1297. catch (FieldValidationException $e) {
  1298. $this->pass('Wrong term causes validation error.');
  1299. }
  1300. }
  1301. /**
  1302. * Test widgets.
  1303. */
  1304. function testTaxonomyTermFieldWidgets() {
  1305. // Create a term in the vocabulary.
  1306. $term = $this->createTerm($this->vocabulary);
  1307. // Display creation form.
  1308. $langcode = LANGUAGE_NONE;
  1309. $this->drupalGet('test-entity/add/test-bundle');
  1310. $this->assertFieldByName("{$this->field_name}[$langcode]", '', 'Widget is displayed.');
  1311. // Submit with some value.
  1312. $edit = array(
  1313. "{$this->field_name}[$langcode]" => array($term->tid),
  1314. );
  1315. $this->drupalPost(NULL, $edit, t('Save'));
  1316. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  1317. $id = $match[1];
  1318. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.');
  1319. // Display the object.
  1320. $entity = field_test_entity_test_load($id);
  1321. $entities = array($id => $entity);
  1322. field_attach_prepare_view('test_entity', $entities, 'full');
  1323. $entity->content = field_attach_view('test_entity', $entity, 'full');
  1324. $this->content = drupal_render($entity->content);
  1325. $this->assertText($term->name, 'Term name is displayed.');
  1326. // Delete the vocabulary and verify that the widget is gone.
  1327. taxonomy_vocabulary_delete($this->vocabulary->vid);
  1328. $this->drupalGet('test-entity/add/test-bundle');
  1329. $this->assertNoFieldByName("{$this->field_name}[$langcode]", '', 'Widget is not displayed.');
  1330. }
  1331. /**
  1332. * Tests that vocabulary machine name changes are mirrored in field definitions.
  1333. */
  1334. function testTaxonomyTermFieldChangeMachineName() {
  1335. // Add several entries in the 'allowed_values' setting, to make sure that
  1336. // they all get updated.
  1337. $this->field['settings']['allowed_values'] = array(
  1338. array(
  1339. 'vocabulary' => $this->vocabulary->machine_name,
  1340. 'parent' => '0',
  1341. ),
  1342. array(
  1343. 'vocabulary' => $this->vocabulary->machine_name,
  1344. 'parent' => '0',
  1345. ),
  1346. array(
  1347. 'vocabulary' => 'foo',
  1348. 'parent' => '0',
  1349. ),
  1350. );
  1351. field_update_field($this->field);
  1352. // Change the machine name.
  1353. $old_name = $this->vocabulary->machine_name;
  1354. $new_name = drupal_strtolower($this->randomName());
  1355. $this->vocabulary->machine_name = $new_name;
  1356. taxonomy_vocabulary_save($this->vocabulary);
  1357. // Check that entity bundles are properly updated.
  1358. $info = entity_get_info('taxonomy_term');
  1359. $this->assertFalse(isset($info['bundles'][$old_name]), 'The old bundle name does not appear in entity_get_info().');
  1360. $this->assertTrue(isset($info['bundles'][$new_name]), 'The new bundle name appears in entity_get_info().');
  1361. // Check that the field instance is still attached to the vocabulary.
  1362. $field = field_info_field($this->field_name);
  1363. $allowed_values = $field['settings']['allowed_values'];
  1364. $this->assertEqual($allowed_values[0]['vocabulary'], $new_name, 'Index 0: Machine name was updated correctly.');
  1365. $this->assertEqual($allowed_values[1]['vocabulary'], $new_name, 'Index 1: Machine name was updated correctly.');
  1366. $this->assertEqual($allowed_values[2]['vocabulary'], 'foo', 'Index 2: Machine name was left untouched.');
  1367. }
  1368. }
  1369. /**
  1370. * Tests a taxonomy term reference field that allows multiple vocabularies.
  1371. */
  1372. class TaxonomyTermFieldMultipleVocabularyTestCase extends TaxonomyWebTestCase {
  1373. protected $instance;
  1374. protected $vocabulary1;
  1375. protected $vocabulary2;
  1376. public static function getInfo() {
  1377. return array(
  1378. 'name' => 'Multiple vocabulary term reference field',
  1379. 'description' => 'Tests term reference fields that allow multiple vocabularies.',
  1380. 'group' => 'Taxonomy',
  1381. );
  1382. }
  1383. function setUp() {
  1384. parent::setUp('field_test');
  1385. $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy'));
  1386. $this->drupalLogin($web_user);
  1387. $this->vocabulary1 = $this->createVocabulary();
  1388. $this->vocabulary2 = $this->createVocabulary();
  1389. // Set up a field and instance.
  1390. $this->field_name = drupal_strtolower($this->randomName());
  1391. $this->field = array(
  1392. 'field_name' => $this->field_name,
  1393. 'type' => 'taxonomy_term_reference',
  1394. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1395. 'settings' => array(
  1396. 'allowed_values' => array(
  1397. array(
  1398. 'vocabulary' => $this->vocabulary1->machine_name,
  1399. 'parent' => '0',
  1400. ),
  1401. array(
  1402. 'vocabulary' => $this->vocabulary2->machine_name,
  1403. 'parent' => '0',
  1404. ),
  1405. ),
  1406. )
  1407. );
  1408. field_create_field($this->field);
  1409. $this->instance = array(
  1410. 'field_name' => $this->field_name,
  1411. 'entity_type' => 'test_entity',
  1412. 'bundle' => 'test_bundle',
  1413. 'widget' => array(
  1414. 'type' => 'options_select',
  1415. ),
  1416. 'display' => array(
  1417. 'full' => array(
  1418. 'type' => 'taxonomy_term_reference_link',
  1419. ),
  1420. ),
  1421. );
  1422. field_create_instance($this->instance);
  1423. }
  1424. /**
  1425. * Tests term reference field and widget with multiple vocabularies.
  1426. */
  1427. function testTaxonomyTermFieldMultipleVocabularies() {
  1428. // Create a term in each vocabulary.
  1429. $term1 = $this->createTerm($this->vocabulary1);
  1430. $term2 = $this->createTerm($this->vocabulary2);
  1431. // Submit an entity with both terms.
  1432. $langcode = LANGUAGE_NONE;
  1433. $this->drupalGet('test-entity/add/test-bundle');
  1434. $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is displayed.');
  1435. $edit = array(
  1436. "{$this->field_name}[$langcode][]" => array($term1->tid, $term2->tid),
  1437. );
  1438. $this->drupalPost(NULL, $edit, t('Save'));
  1439. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  1440. $id = $match[1];
  1441. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.');
  1442. // Render the entity.
  1443. $entity = field_test_entity_test_load($id);
  1444. $entities = array($id => $entity);
  1445. field_attach_prepare_view('test_entity', $entities, 'full');
  1446. $entity->content = field_attach_view('test_entity', $entity, 'full');
  1447. $this->content = drupal_render($entity->content);
  1448. $this->assertText($term1->name, 'Term 1 name is displayed.');
  1449. $this->assertText($term2->name, 'Term 2 name is displayed.');
  1450. // Delete vocabulary 2.
  1451. taxonomy_vocabulary_delete($this->vocabulary2->vid);
  1452. // Re-render the content.
  1453. $entity = field_test_entity_test_load($id);
  1454. $entities = array($id => $entity);
  1455. field_attach_prepare_view('test_entity', $entities, 'full');
  1456. $entity->content = field_attach_view('test_entity', $entity, 'full');
  1457. $this->plainTextContent = FALSE;
  1458. $this->content = drupal_render($entity->content);
  1459. // Term 1 should still be displayed; term 2 should not be.
  1460. $this->assertText($term1->name, 'Term 1 name is displayed.');
  1461. $this->assertNoText($term2->name, 'Term 2 name is not displayed.');
  1462. // Verify that field and instance settings are correct.
  1463. $field_info = field_info_field($this->field_name);
  1464. $this->assertEqual(sizeof($field_info['settings']['allowed_values']), 1, 'Only one vocabulary is allowed for the field.');
  1465. // The widget should still be displayed.
  1466. $this->drupalGet('test-entity/add/test-bundle');
  1467. $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is still displayed.');
  1468. // Term 1 should still pass validation.
  1469. $edit = array(
  1470. "{$this->field_name}[$langcode][]" => array($term1->tid),
  1471. );
  1472. $this->drupalPost(NULL, $edit, t('Save'));
  1473. }
  1474. }
  1475. /**
  1476. * Test taxonomy token replacement in strings.
  1477. */
  1478. class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase {
  1479. public static function getInfo() {
  1480. return array(
  1481. 'name' => 'Taxonomy token replacement',
  1482. 'description' => 'Generates text using placeholders for dummy content to check taxonomy token replacement.',
  1483. 'group' => 'Taxonomy',
  1484. );
  1485. }
  1486. function setUp() {
  1487. parent::setUp();
  1488. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
  1489. $this->drupalLogin($this->admin_user);
  1490. $this->vocabulary = $this->createVocabulary();
  1491. $this->langcode = LANGUAGE_NONE;
  1492. $field = array(
  1493. 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
  1494. 'type' => 'taxonomy_term_reference',
  1495. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1496. 'settings' => array(
  1497. 'allowed_values' => array(
  1498. array(
  1499. 'vocabulary' => $this->vocabulary->machine_name,
  1500. 'parent' => 0,
  1501. ),
  1502. ),
  1503. ),
  1504. );
  1505. field_create_field($field);
  1506. $this->instance = array(
  1507. 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
  1508. 'bundle' => 'article',
  1509. 'entity_type' => 'node',
  1510. 'widget' => array(
  1511. 'type' => 'options_select',
  1512. ),
  1513. 'display' => array(
  1514. 'default' => array(
  1515. 'type' => 'taxonomy_term_reference_link',
  1516. ),
  1517. ),
  1518. );
  1519. field_create_instance($this->instance);
  1520. }
  1521. /**
  1522. * Creates some terms and a node, then tests the tokens generated from them.
  1523. */
  1524. function testTaxonomyTokenReplacement() {
  1525. global $language;
  1526. // Create two taxonomy terms.
  1527. $term1 = $this->createTerm($this->vocabulary);
  1528. $term2 = $this->createTerm($this->vocabulary);
  1529. // Edit $term2, setting $term1 as parent.
  1530. $edit = array();
  1531. $edit['name'] = '<blink>Blinking Text</blink>';
  1532. $edit['parent[]'] = array($term1->tid);
  1533. $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));
  1534. // Create node with term2.
  1535. $edit = array();
  1536. $node = $this->drupalCreateNode(array('type' => 'article'));
  1537. $edit[$this->instance['field_name'] . '[' . $this->langcode . '][]'] = $term2->tid;
  1538. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  1539. // Generate and test sanitized tokens for term1.
  1540. $tests = array();
  1541. $tests['[term:tid]'] = $term1->tid;
  1542. $tests['[term:name]'] = check_plain($term1->name);
  1543. $tests['[term:description]'] = check_markup($term1->description, $term1->format);
  1544. $tests['[term:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE));
  1545. $tests['[term:node-count]'] = 0;
  1546. $tests['[term:parent:name]'] = '[term:parent:name]';
  1547. $tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name);
  1548. foreach ($tests as $input => $expected) {
  1549. $output = token_replace($input, array('term' => $term1), array('language' => $language));
  1550. $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', array('%token' => $input)));
  1551. }
  1552. // Generate and test sanitized tokens for term2.
  1553. $tests = array();
  1554. $tests['[term:tid]'] = $term2->tid;
  1555. $tests['[term:name]'] = check_plain($term2->name);
  1556. $tests['[term:description]'] = check_markup($term2->description, $term2->format);
  1557. $tests['[term:url]'] = url('taxonomy/term/' . $term2->tid, array('absolute' => TRUE));
  1558. $tests['[term:node-count]'] = 1;
  1559. $tests['[term:parent:name]'] = check_plain($term1->name);
  1560. $tests['[term:parent:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE));
  1561. $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]';
  1562. $tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name);
  1563. // Test to make sure that we generated something for each token.
  1564. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
  1565. foreach ($tests as $input => $expected) {
  1566. $output = token_replace($input, array('term' => $term2), array('language' => $language));
  1567. $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', array('%token' => $input)));
  1568. }
  1569. // Generate and test unsanitized tokens.
  1570. $tests['[term:name]'] = $term2->name;
  1571. $tests['[term:description]'] = $term2->description;
  1572. $tests['[term:parent:name]'] = $term1->name;
  1573. $tests['[term:vocabulary:name]'] = $this->vocabulary->name;
  1574. foreach ($tests as $input => $expected) {
  1575. $output = token_replace($input, array('term' => $term2), array('language' => $language, 'sanitize' => FALSE));
  1576. $this->assertEqual($output, $expected, format_string('Unsanitized taxonomy term token %token replaced.', array('%token' => $input)));
  1577. }
  1578. // Generate and test sanitized tokens.
  1579. $tests = array();
  1580. $tests['[vocabulary:vid]'] = $this->vocabulary->vid;
  1581. $tests['[vocabulary:name]'] = check_plain($this->vocabulary->name);
  1582. $tests['[vocabulary:description]'] = filter_xss($this->vocabulary->description);
  1583. $tests['[vocabulary:node-count]'] = 1;
  1584. $tests['[vocabulary:term-count]'] = 2;
  1585. // Test to make sure that we generated something for each token.
  1586. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
  1587. foreach ($tests as $input => $expected) {
  1588. $output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language));
  1589. $this->assertEqual($output, $expected, format_string('Sanitized taxonomy vocabulary token %token replaced.', array('%token' => $input)));
  1590. }
  1591. // Generate and test unsanitized tokens.
  1592. $tests['[vocabulary:name]'] = $this->vocabulary->name;
  1593. $tests['[vocabulary:description]'] = $this->vocabulary->description;
  1594. foreach ($tests as $input => $expected) {
  1595. $output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language, 'sanitize' => FALSE));
  1596. $this->assertEqual($output, $expected, format_string('Unsanitized taxonomy vocabulary token %token replaced.', array('%token' => $input)));
  1597. }
  1598. }
  1599. }
  1600. /**
  1601. * Tests for verifying that taxonomy pages use the correct theme.
  1602. */
  1603. class TaxonomyThemeTestCase extends TaxonomyWebTestCase {
  1604. public static function getInfo() {
  1605. return array(
  1606. 'name' => 'Taxonomy theme switching',
  1607. 'description' => 'Verifies that various taxonomy pages use the expected theme.',
  1608. 'group' => 'Taxonomy',
  1609. );
  1610. }
  1611. function setUp() {
  1612. parent::setUp();
  1613. // Make sure we are using distinct default and administrative themes for
  1614. // the duration of these tests.
  1615. variable_set('theme_default', 'bartik');
  1616. variable_set('admin_theme', 'seven');
  1617. // Create and log in as a user who has permission to add and edit taxonomy
  1618. // terms and view the administrative theme.
  1619. $admin_user = $this->drupalCreateUser(array('administer taxonomy', 'view the administration theme'));
  1620. $this->drupalLogin($admin_user);
  1621. }
  1622. /**
  1623. * Test the theme used when adding, viewing and editing taxonomy terms.
  1624. */
  1625. function testTaxonomyTermThemes() {
  1626. // Adding a term to a vocabulary is considered an administrative action and
  1627. // should use the administrative theme.
  1628. $vocabulary = $this->createVocabulary();
  1629. $this->drupalGet('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add');
  1630. $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page for adding a taxonomy term.");
  1631. // Viewing a taxonomy term should use the default theme.
  1632. $term = $this->createTerm($vocabulary);
  1633. $this->drupalGet('taxonomy/term/' . $term->tid);
  1634. $this->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page for viewing a taxonomy term.");
  1635. // Editing a taxonomy term should use the same theme as adding one.
  1636. $this->drupalGet('taxonomy/term/' . $term->tid . '/edit');
  1637. $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page for editing a taxonomy term.");
  1638. }
  1639. }
  1640. /**
  1641. * Tests the functionality of EntityFieldQuery for taxonomy entities.
  1642. */
  1643. class TaxonomyEFQTestCase extends TaxonomyWebTestCase {
  1644. public static function getInfo() {
  1645. return array(
  1646. 'name' => 'Taxonomy EntityFieldQuery',
  1647. 'description' => 'Verifies operation of a taxonomy-based EntityFieldQuery.',
  1648. 'group' => 'Taxonomy',
  1649. );
  1650. }
  1651. function setUp() {
  1652. parent::setUp();
  1653. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
  1654. $this->drupalLogin($this->admin_user);
  1655. $this->vocabulary = $this->createVocabulary();
  1656. }
  1657. /**
  1658. * Tests that a basic taxonomy EntityFieldQuery works.
  1659. */
  1660. function testTaxonomyEFQ() {
  1661. $terms = array();
  1662. for ($i = 0; $i < 5; $i++) {
  1663. $term = $this->createTerm($this->vocabulary);
  1664. $terms[$term->tid] = $term;
  1665. }
  1666. $query = new EntityFieldQuery();
  1667. $query->entityCondition('entity_type', 'taxonomy_term');
  1668. $result = $query->execute();
  1669. $result = $result['taxonomy_term'];
  1670. asort($result);
  1671. $this->assertEqual(array_keys($terms), array_keys($result), 'Taxonomy terms were retrieved by EntityFieldQuery.');
  1672. // Create a second vocabulary and five more terms.
  1673. $vocabulary2 = $this->createVocabulary();
  1674. $terms2 = array();
  1675. for ($i = 0; $i < 5; $i++) {
  1676. $term = $this->createTerm($vocabulary2);
  1677. $terms2[$term->tid] = $term;
  1678. }
  1679. $query = new EntityFieldQuery();
  1680. $query->entityCondition('entity_type', 'taxonomy_term');
  1681. $query->entityCondition('bundle', $vocabulary2->machine_name);
  1682. $result = $query->execute();
  1683. $result = $result['taxonomy_term'];
  1684. asort($result);
  1685. $this->assertEqual(array_keys($terms2), array_keys($result), format_string('Taxonomy terms from the %name vocabulary were retrieved by EntityFieldQuery.', array('%name' => $vocabulary2->name)));
  1686. }
  1687. }