metatag.with_search_api.test 3.5 KB

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