linkit_search_plugin_taxonomy_term.test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Linkit search plugin taxonomy term.
  5. */
  6. /**
  7. * Test the the taxonomy term search plugin.
  8. */
  9. class LinkitsearchPluginTaxonomyTermTestCase extends LinkitsearchPluginTestCase {
  10. /**
  11. * Definition.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Linkit Search plugin (Taxonomy term)',
  16. 'description' => 'Test the the taxonomy term search plugin.',
  17. 'group' => 'Linkit'
  18. );
  19. }
  20. function setUp($extra_modules = array()) {
  21. parent::setUp(array('taxonomy'));
  22. // Create a basic profile.
  23. $this->createProfile(array(
  24. 'data' => array(
  25. 'search_plugins' => array(
  26. 'entity:taxonomy_term' => array(
  27. 'enabled' => 1,
  28. 'weight' => 0,
  29. ),
  30. ),
  31. 'entity:taxonomy_term' => array(
  32. 'result_description' => '',
  33. 'bundles' => array(),
  34. 'group_by_bundle' => 0,
  35. ),
  36. 'insert_plugin' => array(
  37. 'url_method' => LINKIT_URL_METHOD_RAW,
  38. ),
  39. )
  40. ));
  41. }
  42. /**
  43. * Create and returns a new vocabulary.
  44. */
  45. function createVocabulary() {
  46. // Create a vocabulary.
  47. $vocabulary = new stdClass();
  48. $vocabulary->name = $this->randomName();
  49. $vocabulary->description = $this->randomName();
  50. $vocabulary->machine_name = drupal_strtolower($this->randomName());
  51. $vocabulary->help = '';
  52. $vocabulary->nodes = array('article' => 'article');
  53. $vocabulary->weight = mt_rand(0, 10);
  54. taxonomy_vocabulary_save($vocabulary);
  55. return $vocabulary;
  56. }
  57. /**
  58. * Create and returns a new term in vocabulary $vid.
  59. */
  60. function createTerm($vocabulary) {
  61. $term = new stdClass();
  62. $term->name = $this->search_string . $this->randomName();
  63. $term->description = $this->randomName();
  64. // Use the first available text format.
  65. $term->format = 'plain_text';
  66. $term->vid = $vocabulary->vid;
  67. taxonomy_term_save($term);
  68. return $term;
  69. }
  70. /**
  71. * Test that we get results back which is valid.
  72. */
  73. public function testBasicResults() {
  74. // Create demo vocabulary.
  75. $vocabulary = $this->createVocabulary();
  76. // Create demo terms.
  77. $term_1 = $this->createTerm($vocabulary);
  78. $term_2 = $this->createTerm($vocabulary);
  79. $term_3 = $this->createTerm($vocabulary);
  80. // Call the autocomplete helper method.
  81. $this->autocompleteCall();
  82. // Check that the term names appears in the response.
  83. $this->assertRaw($term_1->name, 'Term was found in the result array.');
  84. $this->assertRaw($term_2->name, 'Term was found in the result array.');
  85. $this->assertRaw($term_3->name, 'Term was found in the result array.');
  86. }
  87. /**
  88. * Test group by bundle.
  89. *
  90. * The actual grouping is made by the javascript later on.
  91. * We just test that the resons contains group info.
  92. */
  93. public function testGroupbyBundle() {
  94. // Create demo vocabulary.
  95. $vocabulary_1 = $this->createVocabulary();
  96. $vocabulary_2 = $this->createVocabulary();
  97. // Create demo terms.
  98. $term_1 = $this->createTerm($vocabulary_1);
  99. $term_2 = $this->createTerm($vocabulary_1);
  100. $term_3 = $this->createTerm($vocabulary_2);
  101. $term_4 = $this->createTerm($vocabulary_2);
  102. // Update the profile to group by bundle.
  103. $this->_profile->data['entity:taxonomy_term']['group_by_bundle'] = 1;
  104. $this->updateProfile();
  105. // Call the autocomplete helper method.
  106. $this->autocompleteCall();
  107. // Assert that the bundle title appear in the response.
  108. $this->assertRaw('"group":"Taxonomy term - ' . $vocabulary_1->name, 'Bundle name in group was found in the result array.');
  109. $this->assertRaw('"group":"Taxonomy term - ' . $vocabulary_1->name, 'Bundle name in group was found in the result array.');
  110. }
  111. /**
  112. * Test bundle filter.
  113. */
  114. public function testBundleFilter() {
  115. // Create demo vocabulary.
  116. $vocabulary_1 = $this->createVocabulary();
  117. $vocabulary_2 = $this->createVocabulary();
  118. // Create demo terms.
  119. $term_1 = $this->createTerm($vocabulary_1);
  120. $term_2 = $this->createTerm($vocabulary_2);
  121. // Update the profile to filter by bundle.
  122. $this->_profile->data['entity:taxonomy_term']['bundles'] = array(
  123. $vocabulary_1->machine_name => $vocabulary_1->machine_name,
  124. );
  125. $this->updateProfile();
  126. // Call the autocomplete helper method.
  127. $this->autocompleteCall();
  128. // Assert that the term title from the bundle included in the filter appear in the response.
  129. $this->assertRaw($term_1->name, 'Term that is included in the filter was found in the result array.');
  130. // Assert that the term title from the bundle that is not included in the filter doesnät appear in the response.
  131. $this->assertNoRaw($term_2->name, 'Term that is not included in the filter was not found in the result array.');
  132. }
  133. /**
  134. * Test result description.
  135. *
  136. * We just test one token.
  137. */
  138. public function testDescription() {
  139. // Create demo vocabulary.
  140. $vocabulary_1 = $this->createVocabulary();
  141. $vocabulary_2 = $this->createVocabulary();
  142. // Create demo terms.
  143. $term_1 = $this->createTerm($vocabulary_1);
  144. $term_2 = $this->createTerm($vocabulary_2);
  145. // Update the profile with a taxonomy_term result description.
  146. $this->_profile->data['entity:taxonomy_term']['result_description'] = 'Term [term:vocabulary]';
  147. $this->updateProfile();
  148. // Call the autocomplete helper method.
  149. $this->autocompleteCall();
  150. // Check that the result description appers in the result.
  151. $this->assertRaw('Term ' . $vocabulary_1->name, 'The result description was found in the result array.');
  152. $this->assertRaw('Term ' . $vocabulary_2->name, 'The result description was found in the result array.');
  153. }
  154. }