facetapi.test 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Facet API module.
  5. */
  6. /**
  7. * Base class for all Facet API test cases.
  8. */
  9. class FacetapiTestCase extends DrupalWebTestCase {
  10. /**
  11. * Overrides DrupalWebTestCase::setUp().
  12. *
  13. * Enables the "block" and "facetapi_test" modules, creates an administrative
  14. * user with permissions to configure the Faceted Search interface.
  15. */
  16. public function setUp() {
  17. $modules = func_get_args();
  18. $modules[] = 'block';
  19. $modules[] = 'facetapi_test';
  20. forward_static_call_array('parent::setUp', $modules);
  21. $this->authenticatedUser = $this->drupalCreateUser();
  22. $this->adminUser = $this->drupalCreateUser(array(
  23. 'administer blocks',
  24. 'administer search',
  25. 'access administration pages',
  26. ));
  27. }
  28. /**
  29. * Overrides DrupalWebTestCase::drupalPost()
  30. *
  31. * Clears static variables on settings submission. This is OK because the
  32. * script execution persists when the form is submitted by the test unlike a
  33. * real form post where the page has to reload.
  34. */
  35. public function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
  36. parent::drupalPost($path, $edit, $submit, $options, $headers, $form_html_id, $extra_post);
  37. if (t('Save configuration') == $submit) {
  38. drupal_static('facetapi_get_searcher_settings', array(), TRUE);
  39. drupal_static('facetapi_get_enabled_facets', array(), TRUE);
  40. }
  41. }
  42. /**
  43. * Returns adapter, realm, and facet objects and base admin path.
  44. *
  45. * Useful with the list() function.
  46. *
  47. * @return array
  48. * An array containing:
  49. * - The facet definition as retrned by facetapi_facet_load().
  50. * - The realm definition as retrned by facetapi_realm_load().
  51. * - The FacetapiAdapter object as returned by facetapi_adapter_load().
  52. * - The base admin path for facet settings forms.
  53. */
  54. public function facetapiLoadObjects($facet_name = 'enabled', $realm_name = 'nodisplay', $searcher = 'facetapi_test') {
  55. return array(
  56. facetapi_facet_load($facet_name, $searcher),
  57. facetapi_realm_load($realm_name),
  58. facetapi_adapter_load($searcher),
  59. "admin/config/search/facetapi/$searcher/$realm_name/$facet_name",
  60. );
  61. }
  62. /**
  63. * Enables a facet via the UI.
  64. */
  65. public function facetapiEnableFacet($facet_name = 'enabled', $realm_name = 'block', $searcher = 'facetapi_test') {
  66. // Capture current user, switch to admin user if necessary.
  67. $account = $this->loggedInUser;
  68. if ($account != $this->adminUser) {
  69. $this->drupalLogin($this->adminUser);
  70. }
  71. // Posts the form, ensures it was successfully submitted.
  72. $path = 'admin/config/search/facetapi_test/facets/' . $realm_name;
  73. $values = array('enabled_facets[' . $facet_name . ']' => $facet_name);
  74. $this->drupalPost($path, $values, t('Save configuration'));
  75. $this->assertRaw(t('The configuration options have been saved.'), t('Facet form successfully submitted.'), 'Facet API');
  76. // Test that the facet was successfully enabled.
  77. $value = facetapi_facet_enabled($searcher, $realm_name, $facet_name);
  78. $this->assertTrue($value, t('Facet enabled via the interface.'), 'Facet API');
  79. // Log back in as original user if necessary.
  80. if ($account != $this->adminUser) {
  81. if ($account) {
  82. $this->drupalLogin($account);
  83. }
  84. else {
  85. $this->drupalLogout();
  86. }
  87. }
  88. }
  89. /**
  90. * Enables a facet block via the UI.
  91. */
  92. public function facetapiEnableFacetBlock($facet_name = 'enabled', $realm_name = 'block', $searcher = 'facetapi_test') {
  93. // Capture current user, switch to admin user if necessary.
  94. $account = $this->loggedInUser;
  95. if ($account != $this->adminUser) {
  96. $this->drupalLogin($this->adminUser);
  97. }
  98. // Generates the "key" via the facetapi_hash_delta() function.
  99. module_load_include('inc', 'facetapi', 'facetapi.block');
  100. $detla = facetapi_build_delta($searcher, $realm_name, $facet_name);
  101. $key = 'facetapi_' . facetapi_hash_delta($detla);
  102. // Enables the facet in the "sidebar_first" region.
  103. $edit = array('blocks[' . $key . '][region]' => 'sidebar_first');
  104. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  105. // Log back in as original user if necessary.
  106. if ($account != $this->adminUser) {
  107. if ($account) {
  108. $this->drupalLogin($account);
  109. }
  110. else {
  111. $this->drupalLogout();
  112. }
  113. }
  114. }
  115. /**
  116. * Sets debug message flagging which issue was just tested.
  117. *
  118. * @param string $issue
  119. * The URL to the issue on Drupal.org.
  120. */
  121. public function facetapiIssueMessage($issue) {
  122. $href = check_plain($issue);
  123. $link = '<a href="' . $href . '">' . $href . '</a>';
  124. $this->pass(t('Tested issue !link', array('!link' => $link)), 'Debug');
  125. }
  126. }
  127. /**
  128. * Test cases for operations taken through the admin UI.
  129. */
  130. class FacetapiAdminInterfaceTestCase extends FacetapiTestCase {
  131. protected $authenticatedUser;
  132. protected $adminUser;
  133. public static function getInfo() {
  134. return array(
  135. 'name' => 'Administrative UI',
  136. 'description' => 'Tests the UI for Facet API administrative pages.',
  137. 'group' => 'Facet API',
  138. );
  139. }
  140. /**
  141. * Tests access to callbacks.
  142. *
  143. * @todo Test invalid adapters, realms, facets. Test multiple realms.
  144. */
  145. public function testFormAccess() {
  146. $paths = array(
  147. 'admin/config/search/facetapi_test/facets' => t('realm settings'),
  148. 'admin/config/search/facetapi/facetapi_test/block/enabled/edit' => t('display settings'),
  149. 'admin/config/search/facetapi/facetapi_test/block/enabled/dependencies' => t('dependency settings'),
  150. 'admin/config/search/facetapi/facetapi_test/block/enabled/filters' => t('filter settings'),
  151. 'admin/config/search/facetapi/facetapi_test/block/enabled/export' => t('export settings'),
  152. 'admin/config/search/facetapi/facetapi_test/block/enabled/revert' => t('revert settings')
  153. );
  154. // Test wheter unprivileged user is denied access to forms.
  155. $this->drupalLogin($this->authenticatedUser);
  156. foreach ($paths as $path => $form_name) {
  157. $this->drupalGet($path);
  158. $this->assertResponse(403, t('Unprivileged user does not have access to the @form-name form.', array('@form-name' => $form_name)), 'Facet API');
  159. }
  160. // Common message for privileged access checks.
  161. $privileged_message = t('Privileged user with "@permission" permission is granted access to the @form-name form.');
  162. // Test whether privileged user is granted access for forms.
  163. // NOTE: $this->adminUser has "administer search" permission.
  164. $this->drupalLogin($this->adminUser);
  165. foreach ($paths as $path => $form_name) {
  166. $this->drupalGet($path);
  167. $args = array('@permission' => 'administer search', '@form-name' => $form_name);
  168. $this->assertResponse(200, t($privileged_message, $args));
  169. }
  170. // Tests whether privileged user is granted access for forms.
  171. // Create another user with the "administer facets" permission, test whether
  172. $facet_admin_user = $this->drupalCreateUser(array('administer facets'));
  173. $this->drupalLogin($facet_admin_user);
  174. foreach ($paths as $path => $form_name) {
  175. $this->drupalGet($path);
  176. $args = array('@permission' => 'administer facets', '@form-name' => $form_name);
  177. $this->assertResponse(200, t($privileged_message, $args));
  178. }
  179. }
  180. /**
  181. * Tests enabling a facet via the UI.
  182. */
  183. public function testEnableFacet() {
  184. list($facet) = $this->facetapiLoadObjects();
  185. // Enable the "enabled" facet and block via UI.
  186. $this->drupalLogin($this->adminUser);
  187. $this->facetapiEnableFacet();
  188. $this->facetapiEnableFacetBlock();
  189. // Test that block is positioned on the search page.
  190. $this->drupalLogin($this->authenticatedUser);
  191. $this->drupalGet('facetapi_test/search');
  192. $raw = t('Filter by @title:', array('@title' => drupal_strtolower($facet['label'])));
  193. $this->assertRaw($raw, t('Facet block displayed on search page.'), 'Facet API');
  194. }
  195. /**
  196. * Tests display form redirects.
  197. */
  198. public function testDisplayFormRedirect() {
  199. list($facet, $realm, $adapter, $base_path) = $this->facetapiLoadObjects();
  200. $path = "$base_path/edit";
  201. $button_text = t('Save and go back to realm settings');
  202. $this->drupalLogin($this->adminUser);
  203. $this->drupalPost($path, array(), $button_text);
  204. $this->assertRaw($realm['label'], t('Form is redirected to realm settings form when the "@button-text" button is submitted.', array('@button-text' => $button_text)));
  205. }
  206. /**
  207. * Tests that an admin can selet a valid widget.
  208. *
  209. * In addition to checking that a valid widget can be selected, it also tests
  210. * that an invalid widget cannot be selected by choosing one that does not
  211. * support the "term" query type.
  212. */
  213. public function testWidgetSelect() {
  214. $this->drupalLogin($this->adminUser);
  215. list($facet, $realm, $adapter, $base_path) = $this->facetapiLoadObjects();
  216. $path = "$base_path/edit";
  217. // Ensure that a valid widget can be selected.
  218. $this->drupalGet($path);
  219. $values = array('widget' => 'facetapi_checkbox_links');
  220. $this->drupalPost($path, $values, t('Save configuration'));
  221. $settings = $adapter->getFacetSettings($facet, $realm);
  222. $this->assertEqual($settings->settings['widget'], 'facetapi_checkbox_links', t('The facetapi_checkbox_links widget was successfully selected.'), 'Facet API');
  223. // Ensure that an invalid widget cannot be selected.
  224. $this->drupalGet($path);
  225. $values = array('widget' => 'facetapi_nonterm');
  226. $this->drupalPost($path, $values, t('Save configuration'));
  227. $validation_message = t('The widget does not support the %type query type', array('%type' => 'term'));
  228. $this->assertRaw($validation_message, t('An invalid widget cannot be selected.'), 'Facet API');
  229. $settings = $adapter->getFacetSettings($facet, $realm);
  230. $this->assertNotEqual($settings->settings['widget'], 'facetapi_nonterm', t('An invalid widget does not get saved.'), 'Facet API');
  231. }
  232. }
  233. class FacetapiSearchPageInterfaceTestCase extends FacetapiTestCase {
  234. protected $authenticatedUser;
  235. protected $adminUser;
  236. public static function getInfo() {
  237. return array(
  238. 'name' => 'Search page UI',
  239. 'description' => 'Tests the UI for search pages.',
  240. 'group' => 'Facet API',
  241. );
  242. }
  243. public function testFormAccess() {
  244. // Enable facet and position block.
  245. $this->drupalLogin($this->adminUser);
  246. $this->facetapiEnableFacet();
  247. $this->facetapiEnableFacetBlock();
  248. // Logs in as a normal user.
  249. $this->drupalLogin($this->authenticatedUser);
  250. // Tests breadcrumb trail when search keys are in the path.
  251. $path = 'facetapi_test/search/testkeys';
  252. $options = array('query' => array('f' => array(0 => 'enabled:testone')));
  253. $this->drupalGet($path, $options);
  254. $this->clickLink('testkeys');
  255. $this->assertUrl($path);
  256. // Tests breadcrumb trail when search keys are in the query string.
  257. $path = 'facetapi_test/search';
  258. $options = array('query' => array('keys' => 'testkeys', 'f' => array(0 => 'enabled:testone')));
  259. $this->drupalGet($path, $options);
  260. $this->clickLink('testkeys');
  261. $this->assertUrl($path, array('query' => array('keys' => 'testkeys')));
  262. }
  263. }
  264. class FacetapiBugFixTestCase extends FacetapiTestCase {
  265. protected $authenticatedUser;
  266. protected $adminUser;
  267. public static function getInfo() {
  268. return array(
  269. 'name' => 'Bug Fixes',
  270. 'description' => 'Tests fixed bugs to prevent regressions.',
  271. 'group' => 'Facet API',
  272. );
  273. }
  274. /**
  275. * Tests bug fixed at http://drupal.org/node/1209490.
  276. */
  277. public function testEnableFacetWithColon() {
  278. $this->facetapiEnableFacet('colon:test');
  279. $this->facetapiIssueMessage('http://drupal.org/node/1209490');
  280. }
  281. /**
  282. * Tests bug fixed at http://drupal.org/node/1443340.
  283. */
  284. public function testValidDefaults() {
  285. list($facet, $realm, $adapter, $base_path) = $this->facetapiLoadObjects('defaults');
  286. $this->drupalLogin($this->adminUser);
  287. $path = "$base_path/edit";
  288. $values = array('widget' => 'facetapi_nonterm');
  289. $this->drupalGet($path);
  290. $this->drupalPost($path, $values, t('Save configuration'));
  291. $settings = $adapter->getFacetSettingsGlobal($facet);
  292. $message = t('Defaults settings not hard coded to "term" and FACETAPI_OPERATOR_AND.');
  293. $this->assertEqual($settings->settings['query_type'], 'nonterm', $message, 'Facet API');
  294. $this->facetapiIssueMessage('http://drupal.org/node/1443340');
  295. }
  296. }
  297. /**
  298. * Test cases for low level API functions.
  299. */
  300. class FacetapiApiFunctions extends FacetapiTestCase {
  301. protected $authenticatedUser;
  302. protected $adminUser;
  303. public static function getInfo() {
  304. return array(
  305. 'name' => 'API Functions',
  306. 'description' => 'Tests low level API functions.',
  307. 'group' => 'Facet API',
  308. );
  309. }
  310. /**
  311. * Tests the facetapi_facet_enabled() function.
  312. *
  313. * @see facetapi_facet_enabled()
  314. */
  315. public function testFacetEnabled() {
  316. list($facet, $realm, $adapter) = $this->facetapiLoadObjects();
  317. // Ensure the facet is disabled, which it is by default.
  318. $enabled = facetapi_facet_enabled('facetapi_test', 'nodisplay', 'enabled');
  319. $this->assertFalse($enabled, t('Facet flagged as disabled by facetapi_facet_enabled().'), 'Facet API');
  320. // Enable the facet, ensure the API function flags it as enabled.
  321. facetapi_save_facet_enabled($adapter, $realm, $facet);
  322. $enabled = facetapi_facet_enabled('facetapi_test', 'nodisplay', 'enabled');
  323. $this->assertTrue($enabled, t('Facet flagged as enabled by facetapi_facet_enabled().'), 'Facet API');
  324. }
  325. /**
  326. * Tests facetapi_save_facet_status() API functions.
  327. *
  328. * @see facetapi_save_facet_status()
  329. * @see facetapi_save_facet_enabled()
  330. * @see facetapi_save_facet_disabled()
  331. */
  332. public function testSaveFacetStatus() {
  333. list($facet, $realm, $adapter) = $this->facetapiLoadObjects();
  334. facetapi_save_facet_enabled($adapter, $realm, $facet);
  335. $enabled = facetapi_facet_enabled('facetapi_test', 'nodisplay', 'enabled');
  336. $this->assertTrue($enabled, t('Facet enabled by facetapi_save_facet_enabled().'), 'Facet API');
  337. facetapi_save_facet_disabled($adapter, $realm, $facet);
  338. $enabled = facetapi_facet_enabled('facetapi_test', 'nodisplay', 'enabled');
  339. $this->assertFalse($enabled, t('Facet disabled by facetapi_save_facet_disabled().'), 'Facet API');
  340. }
  341. /**
  342. * Tests facetapi_set_facet_status() API functions.
  343. *
  344. * @see facetapi_set_facet_status()
  345. * @see facetapi_set_facet_enabled()
  346. * @see facetapi_set_facet_disabled()
  347. */
  348. public function testSetFacetStatus() {
  349. list($facet, $realm, $adapter) = $this->facetapiLoadObjects();
  350. // Facet disabled by default, tests temporarily overriding status. Although
  351. // the setting is disabled, status should be TRUE.
  352. facetapi_set_facet_enabled('facetapi_test', 'nodisplay', 'enabled');
  353. $enabled = facetapi_facet_enabled('facetapi_test', 'nodisplay', 'enabled');
  354. $this->assertTrue($enabled, t('Facet temporarily enabled by facetapi_set_facet_disabled().'), 'Facet API');
  355. // Saves facet as enabled, tests temporarily overriding status. Although the
  356. // setting is enabled, status should be FALSE.
  357. facetapi_save_facet_enabled($adapter, $realm, $facet);
  358. facetapi_set_facet_disabled('facetapi_test', 'nodisplay', 'enabled');
  359. $enabled = facetapi_facet_enabled('facetapi_test', 'nodisplay', 'enabled');
  360. $this->assertFalse($enabled, t('Facet temporarily disabled by facetapi_set_facet_disabled().'), 'Facet API');
  361. }
  362. /**
  363. * Tests the facetapi_translate_string() functionality.
  364. *
  365. * The Facet API Test module is not set as the translator module, so we do it
  366. * by setting the variable in this function. This prevetns it from interfering
  367. * with other tests.
  368. *
  369. * @see facetapi_translate_string().
  370. */
  371. public function testTranslateString() {
  372. // Sets Facet API Test as the translator module.
  373. variable_set('facetapi:translator_module', 'facetapi_test');
  374. $translated = facetapi_translate_string('name', 'untranslated');
  375. $array = unserialize($translated);
  376. $this->assertEqual($array['return'], 'translated', t('String translated by facetapi_translate_string().'), 'Facet API');
  377. }
  378. /**
  379. * Tests the facetapi_*_active_searcher() functions.
  380. *
  381. * @see facetapi_add_active_searcher().
  382. * @see facetapi_is_active_searcher().
  383. */
  384. public function testActiveSearcher() {
  385. // Searcher is not active by default, test that context is FALSE.
  386. $active = facetapi_is_active_searcher('facetapi_test');
  387. $this->assertFalse($active, t('Searcher correctly identified as inactive.'), 'Facet API');
  388. // Sets active searcher, test that context is TRUE.
  389. facetapi_add_active_searcher('facetapi_test');
  390. $active = facetapi_is_active_searcher('facetapi_test');
  391. $this->assertTrue($active, t('Searcher correctly identified as active.'), 'Facet API');
  392. }
  393. /**
  394. * Tests the requirements system for property existence.
  395. *
  396. * @see facetapi_check_requirements()
  397. * @see facetapi_requirement_property_set()
  398. * @see facetapi_requirement_realm_property_set()
  399. * @see facetapi_requirement_facet_property_set()
  400. */
  401. public function testPropertySetRequirements() {
  402. list($facet, $realm, $adapter) = $this->facetapiLoadObjects();
  403. $requirements = array(
  404. 'facetapi_requirement_facet_property_set' => array('label' => TRUE),
  405. );
  406. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  407. $this->assertTrue($passed, t('Requirements pass when an existing facet property is required to be set.'), 'Facet API');
  408. $requirements = array(
  409. 'facetapi_requirement_facet_property_set' => array('label' => FALSE),
  410. );
  411. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  412. $this->assertFalse($passed, t('Requirements fail when an existing facet property is required to not be set.'), 'Facet API');
  413. $requirements = array(
  414. 'facetapi_requirement_facet_property_set' => array('nonsense' => FALSE),
  415. );
  416. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  417. $this->assertTrue($passed, t('Requirements pass when a non-existent facet property is required not to be set.'), 'Facet API');
  418. $requirements = array(
  419. 'facetapi_requirement_facet_property_set' => array('nonsense' => TRUE),
  420. );
  421. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  422. $this->assertFalse($passed, t('Requirements fail when a facet property that is not set is required.'), 'Facet API');
  423. $requirements = array(
  424. 'facetapi_requirement_realm_property_set' => array('label' => TRUE),
  425. );
  426. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  427. $this->assertTrue($passed, t('Requirements pass when an existing realm property is required to be set.'), 'Facet API');
  428. $requirements = array(
  429. 'facetapi_requirement_realm_property_set' => array('label' => FALSE),
  430. );
  431. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  432. $this->assertFalse($passed, t('Requirements fail when an existing realm property is required to not be set.'), 'Facet API');
  433. $requirements = array(
  434. 'facetapi_requirement_realm_property_set' => array('nonsense' => FALSE),
  435. );
  436. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  437. $this->assertTrue($passed, t('Requirements pass when a non-existent realm property is required not to be set.'), 'Facet API');
  438. $requirements = array(
  439. 'facetapi_requirement_realm_property_set' => array('nonsense' => TRUE),
  440. );
  441. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  442. $this->assertFalse($passed, t('Requirements fail when a realm property that is not set is required.'), 'Facet API');
  443. }
  444. /**
  445. * Tests the requirements system for property equality.
  446. *
  447. * @see facetapi_check_requirements()
  448. * @see facetapi_requirement_property()
  449. * @see facetapi_requirement_realm_property()
  450. * @see facetapi_requirement_facet_property()
  451. */
  452. public function testPropertyRequirements() {
  453. list($facet, $realm, $adapter) = $this->facetapiLoadObjects();
  454. $requirements = array(
  455. 'facetapi_requirement_facet_property' => array('label' => t('Enabled facet')),
  456. );
  457. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  458. $this->assertTrue($passed, t('Requirements pass when a testing whether a facet property equals the same value.'), 'Facet API');
  459. $requirements = array(
  460. 'facetapi_requirement_facet_property' => array('label' => 'Does not match'),
  461. );
  462. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  463. $this->assertFalse($passed, t('Requirements fail when a testing whether a facet property equals a different value.'), 'Facet API');
  464. $requirements = array(
  465. 'facetapi_requirement_realm_property' => array('label' => t('No display')),
  466. );
  467. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  468. $this->assertTrue($passed, t('Requirements pass when a testing whether a realm property equals the same value.'), 'Facet API');
  469. $requirements = array(
  470. 'facetapi_requirement_realm_property' => array('label' => 'Does not match'),
  471. );
  472. $passed = facetapi_check_requirements($requirements, $realm, $facet);
  473. $this->assertFalse($passed, t('Requirements fail when a testing whether a realm property equals a different value.'), 'Facet API');
  474. }
  475. /**
  476. * Tests the requirements system for hierarchies.
  477. *
  478. * @see facetapi_check_requirements()
  479. * @see facetapi_requirement_facet_property_set()
  480. * @see facetapi_requirement_facet_hierarchical()
  481. */
  482. public function testHierarchicalRequirements() {
  483. list($e_facet, $realm, $adapter) = $this->facetapiLoadObjects();
  484. $h_facet = facetapi_facet_load('hierarchical', 'facetapi_test');
  485. // Tests facetapi_requirement_facet_hierarchical(). Builds the requirements
  486. // array for the TRUE and FALSE checks.
  487. $requirements_t = array('facetapi_requirement_facet_hierarchical' => TRUE);
  488. $requirements_f = array('facetapi_requirement_facet_hierarchical' => FALSE);
  489. $passed = facetapi_check_requirements($requirements_t, $realm, $h_facet);
  490. $this->assertTrue($passed, t('Requirements pass when checking if a hierarchical facet is hierarchical.'), 'Facet API');
  491. $passed = facetapi_check_requirements($requirements_f, $realm, $h_facet);
  492. $this->assertFalse($passed, t('Requirements fail when checking if a hierarchical facet is not hierarchical.'), 'Facet API');
  493. $passed = facetapi_check_requirements($requirements_f, $realm, $e_facet);
  494. $this->assertTrue($passed, t('Requirements pass when checking if a non-hierarchical facet is not hierarchical.'), 'Facet API');
  495. $passed = facetapi_check_requirements($requirements_t, $realm, $e_facet);
  496. $this->assertFalse($passed, t('Requirements fail when checking if a non-hierarchical facet is hierarchical.'), 'Facet API');
  497. }
  498. /**
  499. * Tests the facetapi_access_callback() access callback.
  500. *
  501. * @see facetapi_access_callback().
  502. */
  503. public function testAccessCallback() {
  504. // The $this->adminUser has the "administer search" permission.
  505. $this->drupalLogin($this->adminUser);
  506. $access = facetapi_access_callback($this->loggedInUser);
  507. $this->assertTrue($access, t('The facetapi_access_callback() function returns TRUE for users with the "administer search" permission.'), 'Facet API');
  508. // Create another user with the "administer facets" permission.
  509. $facet_admin_user = $this->drupalCreateUser(array('administer facets'));
  510. $this->drupalLogin($facet_admin_user);
  511. $access = facetapi_access_callback($this->loggedInUser);
  512. $this->assertTrue($access, t('The facetapi_access_callback() function returns TRUE for users with the "administer facets" permission.'), 'Facet API');
  513. // Users without either permission should be denied access.
  514. $this->drupalLogin($this->authenticatedUser);
  515. $access = facetapi_access_callback($this->loggedInUser);
  516. $this->assertFalse($access, t('The facetapi_access_callback() function returns FALSE for users with neither the "administer search" nor "administer facets" permissions.'), 'Facet API');
  517. }
  518. /**
  519. * Tests the loading of the adapter.
  520. *
  521. * Performs three tests:
  522. * - A valid adapter can be loaded.
  523. * - An invalid adapter returns FALSE.
  524. * - Only one instance per searcher is loaded.
  525. */
  526. public function testAdapterLoad() {
  527. // Loads a valid adapter.
  528. $adapter1 = facetapi_adapter_load('facetapi_test');
  529. $value = ($adapter1 instanceof FacetapiAdapter);
  530. $this->assertTrue($value, t('FacetapiAdapter object loaded via facetapi_adapter_load().'), 'Facet API');
  531. // Loads an invalid adapter.
  532. $adapter_bad = facetapi_adapter_load('nonsense');
  533. $this->assertFalse($adapter_bad, t('FacetapiAdapter object loaded via facetapi_adapter_load().'), 'Facet API');
  534. // Sets a semaphore to see if singleton pattern is implemented.\
  535. $adapter1->semaphore = 'test';
  536. $adapter2 = facetapi_adapter_load('facetapi_test');
  537. $value = (isset($adapter2->semaphore) && 'test' == $adapter2->semaphore);
  538. $this->assertTrue($value, t('Singleton pattern implemented by facetapi_adapter_load().'), 'Facet API');
  539. }
  540. /**
  541. * Test active item processing via the url processor plugin.
  542. *
  543. * @see FacetapiUrlProcessorStandard
  544. * @see FacetapiUrlProcessorStandard::fetchParams()
  545. * @see FacetapiUrlProcessorStandard::normalizeParams()
  546. * @see FacetapiUrlProcessorStandard::setParams()
  547. * @see FacetapiUrlProcessorStandard::getParams()
  548. * @see FacetapiAdapter::initUrlProcessor()
  549. * @see FacetapiAdapter::setParams()
  550. * @see FacetapiAdapter::processActiveItems()
  551. * @see FacetapiAdapter::getAllActiveItems()
  552. */
  553. public function testUrlProcessorDataProcessing() {
  554. list($facet, $realm, $adapter) = $this->facetapiLoadObjects();
  555. facetapi_save_facet_enabled($adapter, $realm, $facet);
  556. // Capture real $_GET.
  557. $get = $_GET;
  558. // Fake the query string.
  559. $_GET = array(
  560. 'q' => array(),
  561. 'page' => 1,
  562. 'f' => array('enabled:testone', 'disabled:testfour', 'enabled:testthree'),
  563. );
  564. // Ensure that the adapter loads the URL processor plugin.
  565. $url_processor = $adapter->getUrlProcessor();
  566. $passed = ($url_processor instanceof FacetapiUrlProcessorStandard);
  567. $this->assertTrue($passed, t('An instance of FacetapiUrlProcessorStandard is returned by the adapter.'), 'Facet API');
  568. // Test fetching params.
  569. $fetched = $url_processor->fetchParams();
  570. $this->assertEqual($fetched, $_GET, t('Parameters fetched from $_GET by FacetapiUrlProcessorStandard::fetchParams().'), 'Facet API');
  571. // Test normalizing fetched params.
  572. $normalized = $url_processor->normalizeParams($fetched);
  573. $this->assertTrue(!isset($normalized['q']), t('The "q" variable is stripped by FacetapiUrlProcessorStandard::normalizeParams().'), 'Facet API');
  574. $this->assertTrue(!isset($normalized['page']), t('The "page" variable is stripped by FacetapiUrlProcessorStandard::normalizeParams().'), 'Facet API');
  575. // Test setting and getting params.
  576. $url_processor->setParams($normalized);
  577. $params = $url_processor->getParams();
  578. $this->assertEqual($normalized, $params, t('Parameters are able to be set and retrieved by getter and setter methods of FacetapiUrlProcessorStandard.'), 'Facet API');
  579. // Test processing of active items.
  580. $adapter->initUrlProcessor();
  581. $active_items = $adapter->getAllActiveItems();
  582. $true = isset($active_items['enabled:testone']);
  583. $this->assertTrue($true, t('Value for enabled facet extracted as an active item.'), 'Facet API');
  584. $false = isset($active_items['disabled:testfour']);
  585. $this->assertFalse($false, t('Value for disabled facet not extracted as an active item.'), 'Facet API');
  586. // Test re-processing of active items.
  587. $facet_d = facetapi_facet_load('disabled', 'facetapi_test');
  588. facetapi_save_facet_enabled($adapter, $realm, $facet_d);
  589. $adapter->initUrlProcessor();
  590. $active_items = $adapter->getAllActiveItems();
  591. $true = isset($active_items['disabled:testfour']);
  592. $this->assertTrue($true, t('Reprocessed value for enabled facet extracted as an active item.'), 'Facet API');
  593. // Reset real $_GET.
  594. $_GET = $get;
  595. }
  596. /**
  597. * Test query string building via the url processor plugin.
  598. *
  599. * @see FacetapiUrlProcessor::getQueryString()
  600. */
  601. public function testUrlProcessorQueryString() {
  602. list($facet, $realm, $adapter) = $this->facetapiLoadObjects();
  603. facetapi_save_facet_enabled($adapter, $realm, $facet);
  604. // Send params with facet data.
  605. $params = array(
  606. 'f' => array('enabled:testone', 'enabled:testthree'),
  607. );
  608. $adapter->setParams($params);
  609. $url_processor = $adapter->getUrlProcessor();
  610. // Tests activating an additional facet item.
  611. $qstring = $url_processor->getQueryString($facet, array('testtwo'), 0);
  612. $expected = array('f' => array('enabled:testone', 'enabled:testthree', 'enabled:testtwo'));
  613. $this->assertEqual($qstring, $expected, t('Facet item is appended to the query string when activated.'), 'Facet API');
  614. // Tests deactivating an additional facet item.
  615. $qstring = $url_processor->getQueryString($facet, array('testone'), 1);
  616. $expected = array('f' => array('enabled:testthree'));
  617. $this->assertEqual($qstring, $expected, t('Facet item is removed from the query string when deactivated.'), 'Facet API');
  618. }
  619. }