예제 #1
0
  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->installSchema('search_api', array('search_api_item', 'search_api_task'));

    // Create a test server.
    $this->server = Server::create(array(
      'name' => $this->randomString(),
      'id' => $this->randomMachineName(),
      'status' => 1,
      'backend' => 'search_api_test_backend',
    ));
    $this->server->save();

    // Create a test index.
    $this->index = Index::create(array(
      'name' => $this->randomString(),
      'id' => $this->randomMachineName(),
      'status' => 1,
      'datasources' => array('entity:' . $this->testEntityTypeId),
      'tracker' => 'default',
      'server' => $this->server->id(),
      'options' => array('index_directly' => FALSE),
    ));
    $this->index->save();

    Utility::getIndexTaskManager()->addItemsAll($this->index);
  }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     // Enable translation for the entity_test module.
     \Drupal::state()->set('entity_test.translation', TRUE);
     $this->installSchema('search_api', array('search_api_item', 'search_api_task'));
     $this->installEntitySchema('entity_test_mul');
     // Create the default languages.
     $this->installConfig(array('language'));
     $this->langcodes = array();
     for ($i = 0; $i < 3; ++$i) {
         /** @var \Drupal\language\Entity\ConfigurableLanguage $language */
         $language = ConfigurableLanguage::create(array('id' => 'l' . $i, 'label' => 'language - ' . $i, 'weight' => $i));
         $this->langcodes[$i] = $language->getId();
         $language->save();
     }
     // Do not use a batch for tracking the initial items after creating an
     // index when running the tests via the GUI. Otherwise, it seems Drupal's
     // Batch API gets confused and the test fails.
     \Drupal::state()->set('search_api_use_tracking_batch', FALSE);
     // Create a test server.
     $this->server = Server::create(array('name' => 'Test Server', 'id' => 'test_server', 'status' => 1, 'backend' => 'search_api_test_backend'));
     $this->server->save();
     // Create a test index.
     $this->index = Index::create(array('name' => 'Test Index', 'id' => 'test_index', 'status' => 1, 'datasource_settings' => array('entity:' . $this->testEntityTypeId => array('plugin_id' => 'entity:' . $this->testEntityTypeId, 'settings' => array())), 'tracker_settings' => array('default' => array('plugin_id' => 'default', 'settings' => array())), 'server' => $this->server->id(), 'options' => array('index_directly' => FALSE)));
     $this->index->save();
 }
예제 #3
0
 /**
  * Implements the magic __sleep() method.
  *
  * Prevents the server entity from being serialized.
  */
 public function __sleep() {
   if ($this->server) {
     $this->serverId = $this->server->id();
   }
   $properties = array_flip(parent::__sleep());
   unset($properties['server']);
   return array_keys($properties);
 }
예제 #4
0
 /**
  * Tests tracking of items when saving an index through the CLI.
  */
 public function testItemTracking()
 {
     EntityTest::create(array('name' => 'foo bar baz föö smile' . json_decode('"\\u1F601"'), 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('Orange', 'orange', 'örange', 'Orange'), 'category' => 'item_category'))->save();
     EntityTest::create(array('name' => 'foo bar baz föö smile', 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('strawberry', 'llama'), 'category' => 'item_category'))->save();
     // Create a test index.
     /** @var \Drupal\search_api\IndexInterface $index */
     $index = Index::create(array('name' => 'Test index', 'id' => 'index', 'status' => 1, 'datasource_settings' => array('entity:entity_test' => array('plugin_id' => 'entity:entity_test', 'settings' => array())), 'tracker_settings' => array('default' => array('plugin_id' => 'default', 'settings' => array())), 'server' => $this->server->id(), 'options' => array('index_directly' => TRUE)));
     $index->save();
     $total_items = $index->getTrackerInstance()->getTotalItemsCount();
     $indexed_items = $index->getTrackerInstance()->getIndexedItemsCount();
     $this->assertEquals(2, $total_items, 'The 2 items are tracked.');
     $this->assertEquals(0, $indexed_items, 'No items are indexed');
     EntityTest::create(array('name' => 'foo bar baz föö smile', 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('strawberry', 'llama'), 'category' => 'item_category'))->save();
     EntityTest::create(array('name' => 'foo bar baz föö smile', 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('strawberry', 'llama'), 'category' => 'item_category'))->save();
     $total_items = $index->getTrackerInstance()->getTotalItemsCount();
     $indexed_items = $index->getTrackerInstance()->getIndexedItemsCount();
     $this->assertEquals(4, $total_items, 'All 4 items are tracked.');
     $this->assertEquals(2, $indexed_items, '2 items are indexed');
 }
 /**
  * Retrieves the tasks set on the test server.
  *
  * @return object[]
  *   All tasks read from the database for the test server, with numeric keys
  *   starting with 0.
  */
 protected function getServerTasks()
 {
     $tasks = array();
     $select = \Drupal::database()->select('search_api_task', 't');
     $select->fields('t')->orderBy('id')->condition('server_id', $this->server->id());
     foreach ($select->execute() as $task) {
         if ($task->data) {
             $task->data = unserialize($task->data);
         }
         $tasks[] = $task;
     }
     return $tasks;
 }
 /**
  * Asserts enable/disable operations for a search server or index.
  *
  * @param \Drupal\search_api\ServerInterface|\Drupal\search_api\IndexInterface $entity
  *   A search server or index.
  */
 protected function assertEntityStatusChange($entity)
 {
     $this->drupalGet($this->overviewPageUrl);
     $row_class = Html::cleanCssIdentifier($entity->getEntityTypeId() . '-' . $entity->id());
     $this->assertFieldByXPath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-enabled")]', NULL, 'The newly created entity is enabled by default.');
     // The first "Disable" link on the page belongs to our server, the second
     // one to our index.
     $this->clickLink('Disable', $entity instanceof ServerInterface ? 0 : 1);
     // Submit the confirmation form and test that the entity has been disabled.
     $this->drupalPostForm(NULL, array(), 'Disable');
     $this->assertFieldByXPath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-disabled")]', NULL, 'The entity has been disabled.');
     // Now enable the entity and verify that the operation succeeded.
     $this->clickLink('Enable');
     $this->drupalGet($this->overviewPageUrl);
     $this->assertFieldByXPath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-enabled")]', NULL, 'The entity has benn enabled.');
 }
예제 #7
0
 /**
  * Builds the form for the basic server properties.
  *
  * @param \Drupal\search_api\ServerInterface $server
  *   The server that is being created or edited.
  */
 public function buildEntityForm(array &$form, FormStateInterface $form_state, ServerInterface $server)
 {
     $form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css';
     $form['name'] = array('#type' => 'textfield', '#title' => $this->t('Server name'), '#description' => $this->t('Enter the displayed name for the server.'), '#default_value' => $server->label(), '#required' => TRUE);
     $form['id'] = array('#type' => 'machine_name', '#default_value' => $server->id(), '#maxlength' => 50, '#required' => TRUE, '#machine_name' => array('exists' => array($this->getStorage(), 'load'), 'source' => array('name')), '#disabled' => !$server->isNew());
     $form['status'] = array('#type' => 'checkbox', '#title' => $this->t('Enabled'), '#description' => $this->t('Only enabled servers can index items or execute searches.'), '#default_value' => $server->status());
     $form['description'] = array('#type' => 'textarea', '#title' => $this->t('Description'), '#description' => $this->t('Enter a description for the server.'), '#default_value' => $server->getDescription());
     $backend_options = $this->getBackendOptions();
     if ($backend_options) {
         if (count($backend_options) == 1) {
             $server->set('backend', key($backend_options));
         }
         $form['backend'] = array('#type' => 'radios', '#title' => $this->t('Backend'), '#description' => $this->t('Choose a backend to use for this server.'), '#options' => $backend_options, '#default_value' => $server->getBackendId(), '#required' => TRUE, '#ajax' => array('callback' => array(get_class($this), 'buildAjaxBackendConfigForm'), 'wrapper' => 'search-api-backend-config-form', 'method' => 'replace', 'effect' => 'fade'));
     } else {
         drupal_set_message($this->t('There are no backend plugins available for the Search API. Please install a <a href=":url">module that provides a backend plugin</a> to proceed.', array(':url' => Url::fromUri('https://www.drupal.org/node/1254698')->toString())), 'error');
         $form = array();
     }
 }
예제 #8
0
 /**
  * {@inheritdoc}
  */
 public function setServer(ServerInterface $server = NULL)
 {
     $this->serverInstance = $server;
     $this->server = $server ? $server->id() : NULL;
 }
예제 #9
0
 /**
  * Tests whether deleting a server works correctly.
  *
  * @param \Drupal\search_api\ServerInterface $server
  *   The server used for this test.
  */
 public function serverDelete(ServerInterface $server)
 {
     $this->storage->delete(array($server));
     $loaded_server = $this->storage->load($server->id());
     $this->assertNull($loaded_server);
 }
예제 #10
0
 /**
  * {@inheritdoc}
  */
 public function delete(array $ids = NULL, ServerInterface $server = NULL, $index = NULL)
 {
     $delete = $this->database->delete('search_api_task');
     if ($ids) {
         $delete->condition('id', $ids, 'IN');
     }
     if ($server) {
         $delete->condition('server_id', $server->id());
     }
     if ($index) {
         $delete->condition('index_id', $index instanceof IndexInterface ? $index->id() : $index);
     }
     $delete->execute();
 }