linkit_search_plugin_node.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Linkit search plugin node.
  5. */
  6. /**
  7. * Test the the node search plugin.
  8. */
  9. class LinkitsearchPluginNodeTestCase extends LinkitsearchPluginTestCase {
  10. /**
  11. * Definition.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Linkit Search plugin (Node)',
  16. 'description' => 'Test the the node search plugin.',
  17. 'group' => 'Linkit'
  18. );
  19. }
  20. function setUp($extra_modules = array()) {
  21. parent::setUp($extra_modules);
  22. // Create a basic profile.
  23. $this->createProfile(array(
  24. 'data' => array(
  25. 'search_plugins' => array(
  26. 'entity:node' => array(
  27. 'enabled' => 1,
  28. 'weight' => 0,
  29. ),
  30. ),
  31. 'entity:node' => array(
  32. 'result_description' => '',
  33. 'bundles' => array(),
  34. 'group_by_bundle' => 0,
  35. 'include_unpublished' => 0,
  36. ),
  37. 'insert_plugin' => array(
  38. 'url_method' => LINKIT_URL_METHOD_RAW,
  39. ),
  40. )
  41. ));
  42. }
  43. /**
  44. * Test that we get results back which is valid.
  45. */
  46. public function testBasicResults() {
  47. // Create some nodes.
  48. $node_1 = $this->drupalCreateNode(array('type' => 'article', 'title' => $this->search_string . $this->randomName()));
  49. $node_2 = $this->drupalCreateNode(array('type' => 'article', 'title' => $this->search_string . $this->randomName()));
  50. $node_3 = $this->drupalCreateNode(array('type' => 'page', 'title' => $this->search_string . $this->randomName()));
  51. $node_4 = $this->drupalCreateNode(array('type' => 'page', 'title' => $this->search_string . $this->randomName()));
  52. // Call the autocomplete helper method.
  53. $this->autocompleteCall();
  54. // Assert that the node titles appears in the response.
  55. $this->assertRaw($node_1->title, 'Node was found in the result array.');
  56. $this->assertRaw($node_2->title, 'Node was found in the result array.');
  57. $this->assertRaw($node_3->title, 'Node was found in the result array.');
  58. $this->assertRaw($node_4->title, 'Node was found in the result array.');
  59. }
  60. /**
  61. * Test how node states are handled.
  62. */
  63. public function testUnpublishedItems() {
  64. // Create some nodes.
  65. $node_1 = $this->drupalCreateNode(array('type' => 'article', 'title' => $this->search_string . $this->randomName(), 'status' => NODE_NOT_PUBLISHED));
  66. $node_2 = $this->drupalCreateNode(array('type' => 'page', 'title' => $this->search_string . $this->randomName(), 'status' => NODE_NOT_PUBLISHED));
  67. // Call the autocomplete helper method.
  68. $this->autocompleteCall();
  69. // Assert that the unpublished node title doesn't appear in the response.
  70. $this->assertNoRaw($node_1->title, 'Unpublished node was not found in the result array.');
  71. $this->assertNoRaw($node_2->title, 'Unpublished node was not found in the result array.');
  72. // Tell the profile to include unpublished nodes.
  73. $this->_profile->data['entity:node']['include_unpublished'] = 1;
  74. $this->updateProfile();
  75. // In order to see unpublished nodes in the result, the user must also have
  76. // permission to see unpublished nodes.
  77. // We give the user 'bypass node access' permission as we don't crated the
  78. // nodes with this user.
  79. $this->account = $this->drupalCreateUser(array('bypass node access'));
  80. $this->drupalLogin($this->account);
  81. // Call the autocomplete helper method.
  82. $this->autocompleteCall();
  83. // Assert that the unpublished title appears in the response.
  84. $this->assertRaw($node_1->title, 'Unpublished node was found in the result array.');
  85. $this->assertRaw($node_2->title, 'Unpublished node 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 some nodes.
  95. $node_1 = $this->drupalCreateNode(array('type' => 'article', 'title' => $this->search_string . $this->randomName()));
  96. $node_2 = $this->drupalCreateNode(array('type' => 'page', 'title' => $this->search_string . $this->randomName()));
  97. // Get the bundle names.
  98. $node_type_name_1 = node_type_get_name($node_1);
  99. $node_type_name_2 = node_type_get_name($node_2);
  100. // Update the profile to group by bundle.
  101. $this->_profile->data['entity:node']['group_by_bundle'] = 1;
  102. $this->updateProfile();
  103. // Call the autocomplete helper method.
  104. $this->autocompleteCall();
  105. // Assert that the bundle title appear in the response.
  106. $this->assertRaw('"group":"Node - ' . $node_type_name_1, 'Bundle name in group was found in the result array.');
  107. $this->assertRaw('"group":"Node - ' . $node_type_name_2, 'Bundle name in group was found in the result array.');
  108. }
  109. /**
  110. * Test bundle filter.
  111. */
  112. public function testBundleFilter() {
  113. // Create some nodes.
  114. $node_1 = $this->drupalCreateNode(array('type' => 'article', 'title' => $this->search_string . $this->randomName()));
  115. $node_2 = $this->drupalCreateNode(array('type' => 'page', 'title' => $this->search_string . $this->randomName()));
  116. // Update the profile to filter by bundle.
  117. $this->_profile->data['entity:node']['bundles'] = array(
  118. 'article' => 'article',
  119. );
  120. $this->updateProfile();
  121. // Call the autocomplete helper method.
  122. $res = $this->autocompleteCall();
  123. // Assert that the node title from the bundle included in the filter appear in the response.
  124. $this->assertRaw($node_1->title, 'Node that is included in the filter was found in the result array.');
  125. // Assert that the node title from the bundle that is not included in the filter doesnät appear in the response.
  126. $this->assertNoRaw($node_2->title, 'Node that is not included in the filter was not found in the result array.');
  127. }
  128. /**
  129. * Test result description.
  130. *
  131. * We just test one token.
  132. */
  133. public function testDescription() {
  134. // Create some nodes.
  135. $node_1 = $this->drupalCreateNode(array('type' => 'article', 'title' => $this->search_string . $this->randomName()));
  136. $node_2 = $this->drupalCreateNode(array('type' => 'page', 'title' => $this->search_string . $this->randomName()));
  137. // Update the profile with a user result description.
  138. $this->_profile->data['entity:node']['result_description'] = 'Created [node:created:raw]';
  139. $this->updateProfile();
  140. // Call the autocomplete helper method.
  141. $this->autocompleteCall();
  142. // Check that the result description appers in the result.
  143. $this->assertRaw('Created ' . $node_1->created, 'The result description was found in the result array.');
  144. $this->assertRaw('Created ' . $node_2->created, 'The result description was found in the result array.');
  145. }
  146. }