search_api_test.module 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * @file
  4. * Test functions and classes for testing the Search API.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function search_api_test_menu() {
  10. return array(
  11. 'search_api_test/insert' => array(
  12. 'title' => 'Insert item',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('search_api_test_insert_item'),
  15. 'access callback' => TRUE,
  16. ),
  17. 'search_api_test/view/%search_api_test' => array(
  18. 'title' => 'View item',
  19. 'page callback' => 'search_api_test_view',
  20. 'page arguments' => array(2),
  21. 'access callback' => TRUE,
  22. ),
  23. 'search_api_test/touch/%search_api_test' => array(
  24. 'title' => 'Mark item as changed',
  25. 'page callback' => 'search_api_test_touch',
  26. 'page arguments' => array(2),
  27. 'access callback' => TRUE,
  28. ),
  29. 'search_api_test/delete/%search_api_test' => array(
  30. 'title' => 'Delete items',
  31. 'page callback' => 'search_api_test_delete',
  32. 'page arguments' => array(2),
  33. 'access callback' => TRUE,
  34. ),
  35. );
  36. }
  37. /**
  38. * Form callback for inserting an item.
  39. */
  40. function search_api_test_insert_item(array $form, array &$form_state) {
  41. return array(
  42. 'id' => array(
  43. '#type' => 'textfield',
  44. ),
  45. 'title' => array(
  46. '#type' => 'textfield',
  47. ),
  48. 'body' => array(
  49. '#type' => 'textarea',
  50. ),
  51. 'type' => array(
  52. '#type' => 'textfield',
  53. ),
  54. 'keywords' => array(
  55. '#type' => 'textfield',
  56. ),
  57. 'prices' => array(
  58. '#type' => 'textfield',
  59. ),
  60. 'submit' => array(
  61. '#type' => 'submit',
  62. '#value' => t('Save'),
  63. ),
  64. );
  65. }
  66. /**
  67. * Submit callback for search_api_test_insert_item().
  68. */
  69. function search_api_test_insert_item_submit(array $form, array &$form_state) {
  70. form_state_values_clean($form_state);
  71. db_insert('search_api_test')->fields(array_filter($form_state['values']))->execute();
  72. module_invoke_all('entity_insert', search_api_test_load($form_state['values']['id']), 'search_api_test');
  73. }
  74. /**
  75. * Load handler for search_api_test entities.
  76. */
  77. function search_api_test_load($id) {
  78. $ret = entity_load('search_api_test', array($id));
  79. return $ret ? array_shift($ret) : NULL;
  80. }
  81. /**
  82. * Menu callback for displaying search_api_test entities.
  83. */
  84. function search_api_test_view($entity) {
  85. return nl2br(check_plain(print_r($entity, TRUE)));
  86. }
  87. /**
  88. * Menu callback for marking a "search_api_test" entity as changed.
  89. */
  90. function search_api_test_touch($entity) {
  91. module_invoke_all('entity_update', $entity, 'search_api_test');
  92. }
  93. /**
  94. * Menu callback for marking a "search_api_test" entity as changed.
  95. */
  96. function search_api_test_delete($entity) {
  97. db_delete('search_api_test')->condition('id', $entity->id)->execute();
  98. module_invoke_all('entity_delete', $entity, 'search_api_test');
  99. }
  100. /**
  101. * Implements hook_entity_info().
  102. */
  103. function search_api_test_entity_info() {
  104. return array(
  105. 'search_api_test' => array(
  106. 'label' => 'Search API test entity',
  107. 'base table' => 'search_api_test',
  108. 'uri callback' => 'search_api_test_uri',
  109. 'entity keys' => array(
  110. 'id' => 'id',
  111. ),
  112. ),
  113. );
  114. }
  115. /**
  116. * Implements hook_entity_property_info().
  117. */
  118. function search_api_test_entity_property_info() {
  119. $info['search_api_test']['properties'] = array(
  120. 'id' => array(
  121. 'label' => 'ID',
  122. 'type' => 'integer',
  123. 'description' => 'The primary identifier for a server.',
  124. ),
  125. 'title' => array(
  126. 'label' => 'Title',
  127. 'type' => 'text',
  128. 'description' => 'The title of the item.',
  129. 'required' => TRUE,
  130. ),
  131. 'body' => array(
  132. 'label' => 'Body',
  133. 'type' => 'text',
  134. 'description' => 'A text belonging to the item.',
  135. 'sanitize' => 'filter_xss',
  136. 'required' => TRUE,
  137. ),
  138. 'type' => array(
  139. 'label' => 'Type',
  140. 'type' => 'text',
  141. 'description' => 'A string identifying the type of item.',
  142. 'required' => TRUE,
  143. ),
  144. 'parent' => array(
  145. 'label' => 'Parent',
  146. 'type' => 'search_api_test',
  147. 'description' => "The item's parent.",
  148. 'getter callback' => 'search_api_test_parent',
  149. ),
  150. 'keywords' => array(
  151. 'label' => 'Keywords',
  152. 'type' => 'list<string>',
  153. 'description' => 'An optional collection of keywords describing the item.',
  154. 'getter callback' => 'search_api_test_list_callback',
  155. ),
  156. 'prices' => array(
  157. 'label' => 'Prices',
  158. 'type' => 'list<decimal>',
  159. 'description' => 'An optional list of prices.',
  160. 'getter callback' => 'search_api_test_list_callback',
  161. ),
  162. );
  163. return $info;
  164. }
  165. /**
  166. * URI callback for test entity.
  167. */
  168. function search_api_test_uri($entity) {
  169. return array(
  170. 'path' => 'search_api_test/' . $entity->id,
  171. );
  172. }
  173. /**
  174. * Parent callback.
  175. */
  176. function search_api_test_parent($entity) {
  177. return search_api_test_load($entity->id - 1);
  178. }
  179. /**
  180. * List callback.
  181. */
  182. function search_api_test_list_callback($data, array $options, $name) {
  183. if (is_array($data)) {
  184. $res = is_array($data[$name]) ? $data[$name] : explode(',', $data[$name]);
  185. }
  186. else {
  187. $res = is_array($data->$name) ? $data->$name : explode(',', $data->$name);
  188. }
  189. if ($name == 'prices') {
  190. foreach ($res as &$x) {
  191. $x = (float) $x;
  192. }
  193. }
  194. return array_filter($res);
  195. }
  196. /**
  197. * Implements hook_search_api_service_info().
  198. */
  199. function search_api_test_search_api_service_info() {
  200. $services['search_api_test_service'] = array(
  201. 'name' => 'search_api_test_service',
  202. 'description' => 'search_api_test_service description',
  203. 'class' => 'SearchApiTestService',
  204. );
  205. return $services;
  206. }
  207. /**
  208. * Test service class.
  209. */
  210. class SearchApiTestService extends SearchApiAbstractService {
  211. /**
  212. * Overrides SearchApiAbstractService::configurationForm().
  213. *
  214. * Returns a single text field for testing purposes.
  215. */
  216. public function configurationForm(array $form, array &$form_state) {
  217. $form = array(
  218. 'test' => array(
  219. '#type' => 'textfield',
  220. '#title' => 'Test option',
  221. ),
  222. );
  223. if (!empty($this->options)) {
  224. $form['test']['#default_value'] = $this->options['test'];
  225. }
  226. return $form;
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. public function addIndex(SearchApiIndex $index) {
  232. $this->checkErrorState();
  233. }
  234. /**
  235. * {@inheritdoc}
  236. */
  237. public function fieldsUpdated(SearchApiIndex $index) {
  238. $this->checkErrorState();
  239. return db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField() > 0;
  240. }
  241. /**
  242. * {@inheritdoc}
  243. */
  244. public function removeIndex($index) {
  245. $this->checkErrorState();
  246. parent::removeIndex($index);
  247. }
  248. /**
  249. * Implements SearchApiServiceInterface::indexItems().
  250. *
  251. * Indexes items by storing their IDs in the server's options.
  252. *
  253. * If the "search_api_test_indexing_break" variable is set, the item with
  254. * that ID will not be indexed.
  255. */
  256. public function indexItems(SearchApiIndex $index, array $items) {
  257. $this->checkErrorState();
  258. // Refuse to index the item with the same ID as the
  259. // "search_api_test_indexing_break" variable, if it is set.
  260. $exclude = variable_get('search_api_test_indexing_break', 8);
  261. foreach ($items as $id => $item) {
  262. if ($id == $exclude) {
  263. unset($items[$id]);
  264. }
  265. }
  266. $ids = array_keys($items);
  267. $this->options += array('indexes' => array());
  268. $this->options['indexes'] += array($index->machine_name => array());
  269. $this->options['indexes'][$index->machine_name] += drupal_map_assoc($ids);
  270. asort($this->options['indexes'][$index->machine_name]);
  271. $this->server->save();
  272. return $ids;
  273. }
  274. /**
  275. * Overrides SearchApiAbstractService::preDelete().
  276. *
  277. * Overridden so deleteItems() isn't called which would otherwise lead to the
  278. * server being updated and, eventually, to a notice because there is no
  279. * server to be updated anymore.
  280. */
  281. public function preDelete() {}
  282. /**
  283. * {@inheritdoc}
  284. */
  285. public function deleteItems($ids = 'all', SearchApiIndex $index = NULL) {
  286. $this->checkErrorState();
  287. if ($ids == 'all') {
  288. if ($index) {
  289. $this->options['indexes'][$index->machine_name] = array();
  290. }
  291. else {
  292. $this->options['indexes'] = array();
  293. }
  294. }
  295. else {
  296. foreach ($ids as $id) {
  297. unset($this->options['indexes'][$index->machine_name][$id]);
  298. }
  299. }
  300. $this->server->save();
  301. }
  302. /**
  303. * Implements SearchApiServiceInterface::indexItems().
  304. *
  305. * Will ignore all query settings except the range, as only the item IDs are
  306. * indexed.
  307. */
  308. public function search(SearchApiQueryInterface $query) {
  309. $options = $query->getOptions();
  310. $ret = array();
  311. $index_id = $query->getIndex()->machine_name;
  312. if (empty($this->options['indexes'][$index_id])) {
  313. return array(
  314. 'result count' => 0,
  315. 'results' => array(),
  316. );
  317. }
  318. $items = $this->options['indexes'][$index_id];
  319. $min = isset($options['offset']) ? $options['offset'] : 0;
  320. $max = $min + (isset($options['limit']) ? $options['limit'] : count($items));
  321. $i = 0;
  322. $ret['result count'] = count($items);
  323. $ret['results'] = array();
  324. foreach ($items as $id) {
  325. ++$i;
  326. if ($i > $max) {
  327. break;
  328. }
  329. if ($i > $min) {
  330. $ret['results'][$id] = array(
  331. 'id' => $id,
  332. 'score' => 1,
  333. );
  334. }
  335. }
  336. return $ret;
  337. }
  338. /**
  339. * Throws an exception if the "search_api_test_error_state" variable is set.
  340. *
  341. * @throws SearchApiException
  342. * If the "search_api_test_error_state" variable is set.
  343. */
  344. protected function checkErrorState() {
  345. if (variable_get('search_api_test_error_state', FALSE)) {
  346. throw new SearchApiException();
  347. }
  348. }
  349. }