metatag.with_search_api.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Search API integration.
  5. */
  6. /**
  7. * Tests for the Search API integration.
  8. */
  9. class MetatagCoreWithSearchAPITest extends MetatagTestHelper {
  10. /**
  11. * The index used by these tests.
  12. *
  13. * @var SearchApIindex
  14. */
  15. protected $index;
  16. /**
  17. * The ID of the index used by these tests.
  18. *
  19. * @var string
  20. */
  21. protected $indexId;
  22. /**
  23. * The machine name of the created test server.
  24. *
  25. * @var string
  26. */
  27. protected $serverId;
  28. /**
  29. * @inheritdoc
  30. */
  31. public static function getInfo() {
  32. return array(
  33. 'name' => 'Metatag Search API tests',
  34. 'description' => 'Tests the Metatag Search API integration.',
  35. 'group' => 'Metatag',
  36. 'dependencies' => array('ctools', 'token', 'entity', 'search_api'),
  37. );
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. function setUp(array $modules = array()) {
  43. // Needed for Search API.
  44. $modules[] = 'entity';
  45. // The module.
  46. $modules[] = 'search_api';
  47. // Used to help test the integration.
  48. $modules[] = 'metatag_search_test';
  49. parent::setUp($modules);
  50. // Create an admin user and log them in.
  51. $perms = array(
  52. // Needed for the content type.
  53. 'edit any page content',
  54. // Needed for Search API.
  55. 'administer search_api',
  56. );
  57. $this->adminUser = $this->createAdminUser($perms);
  58. // Log in the admin user.
  59. $this->drupalLogin($this->adminUser);
  60. // Create an index.
  61. $this->indexId = 'test_index';
  62. $this->index = entity_create('search_api_index', array(
  63. 'id' => $this->indexId,
  64. 'name' => 'test',
  65. 'machine_name' => 'test_index',
  66. 'enabled' => 1,
  67. 'item_type' => 'node',
  68. 'options' => array(
  69. 'data_alter_callbacks' => array(
  70. 'search_api_metatag_alter_callback' => array(
  71. 'status' => 1,
  72. ),
  73. ),
  74. 'fields' => array(
  75. 'metatag_keywords' => array(
  76. 'type' => 'text',
  77. ),
  78. ),
  79. ),
  80. ));
  81. $this->index->save();
  82. // Set up a Search API server.
  83. $this->serverId = 'test_server';
  84. $values = array(
  85. 'name' => 'Search API test server',
  86. 'machine_name' => $this->serverId,
  87. 'enabled' => 1,
  88. 'description' => 'A server used for testing.',
  89. 'class' => 'metatag_search_test_service',
  90. );
  91. $this->drupalPost('admin/config/search/search_api/add_server', $values, 'Create server');
  92. // Configure the server.
  93. $this->drupalPost(NULL, array(), 'Create server');
  94. $this->assertText(t('The server was successfully created.'));
  95. $this->drupalGet('admin/config/search/search_api');
  96. $values = array(
  97. 'server' => $this->serverId,
  98. );
  99. $this->drupalPost("admin/config/search/search_api/index/{$this->indexId}/edit", $values, 'Save settings');
  100. $this->clickLink(t('enable'));
  101. // Enable meta tags for the 'page' content type.
  102. metatag_entity_type_enable('node', 'page');
  103. }
  104. /**
  105. * Test that the alter callback indexes the keywords.
  106. */
  107. public function testAlter() {
  108. // Add a node with keywords.
  109. $node = $this->drupalCreateNode();
  110. $keywords = 'puppies, rainbows';
  111. $values = array(
  112. 'metatags[' . LANGUAGE_NONE . '][keywords][value]' => $keywords,
  113. );
  114. $this->drupalPost('node/' . $node->nid . '/edit', $values, 'Save');
  115. // Index the node.
  116. $this->drupalPost("admin/config/search/search_api/index/{$this->indexId}", array(), 'Index now');
  117. // Check whether the keywords have been indexed.
  118. $this->assertIdentical(variable_get('metatag_search_test_keywords', FALSE), $keywords);
  119. }
  120. }