/**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     $this->drupalLogin($this->adminUser);
     $this->indexId = 'test_index';
     Index::create(array('name' => 'Test index', 'id' => $this->indexId, 'status' => 1, 'datasource_settings' => array('entity:node' => array('plugin_id' => 'entity:node', 'settings' => array()))))->save();
 }
  /**
   * {@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);
  }
 /**
  * {@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();
 }
Beispiel #4
0
  /**
   * Performs setup tasks before each individual test method is run.
   *
   * Installs commonly used schemas and sets up a search server and an index,
   * with the specified processor enabled.
   *
   * @param string|null $processor
   *   (optional) The plugin ID of the processor that should be set up for
   *   testing.
   */
  public function setUp($processor = NULL) {
    parent::setUp();

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

    $server_name = $this->randomMachineName();
    $this->server = Server::create(array(
      'id' => strtolower($server_name),
      'name' => $server_name,
      'status' => TRUE,
      'backend' => 'search_api_db',
      'backend_config' => array(
        'min_chars' => 3,
        'database' => 'default:default',
      ),
    ));
    $this->server->save();

    $index_name = $this->randomMachineName();
    $this->index = Index::create(array(
      'id' => strtolower($index_name),
      'name' => $index_name,
      'status' => TRUE,
      'datasources' => array('entity:comment', 'entity:node'),
      'server' => $server_name,
      'tracker' => 'default',
    ));
    $this->index->setServer($this->server);
    $this->index->setOption('fields', array(
      'entity:comment/subject' => array(
        'type' => 'text',
      ),
      'entity:node/title' => array(
        'type' => 'text',
      ),
    ));
    if ($processor) {
      $this->index->setOption('processors', array(
        $processor => array(
          'processor_id' => $processor,
          'weights' => array(),
          'settings' => array(),
        ),
      ));

      /** @var \Drupal\search_api\Processor\ProcessorPluginManager $plugin_manager */
      $plugin_manager = \Drupal::service('plugin.manager.search_api.processor');
      $this->processor = $plugin_manager->createInstance($processor, array('index' => $this->index));
    }
    $this->index->save();
    \Drupal::configFactory()
      ->getEditable('search_api.settings')
      ->set('tracking_page_size', 100)
      ->save();
    Utility::getIndexTaskManager()->addItemsAll($this->index);
  }
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     $this->state = $this->container->get('state');
     $this->installEntitySchema('user');
     $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:user'), 'tracker' => 'default', 'server' => $this->server->id(), 'options' => array('index_directly' => FALSE)));
     $this->index->save();
     $this->serverTaskManager = $this->container->get('search_api.server_task_manager');
 }
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     $this->installSchema('system', 'key_value_expire');
     // The server tasks manager is needed when removing a server.
     $mock = $this->getMock('Drupal\\search_api\\Task\\ServerTaskManagerInterface');
     $this->container->set('search_api.server_task_manager', $mock);
     // Create the index object, but don't save it yet since we want to change
     // its settings anyways in every test.
     $this->index = Index::create(array('id' => 'test_index', 'name' => 'Test index', 'tracker_settings' => array('default' => array('plugin_id' => 'default', 'settings' => array())), 'datasource_settings' => array('entity:user' => array('plugin_id' => 'entity:user', 'settings' => array()))));
     // Use a search server as the dependency, since we have that available
     // anyways. The entity type should not matter at all, though.
     $this->dependency = Server::create(array('id' => 'dependency', 'name' => 'Test dependency', 'backend' => 'search_api_test_backend'));
     $this->dependency->save();
 }
Beispiel #7
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');
 }
 /**
  * Performs setup tasks before each individual test method is run.
  *
  * Installs commonly used schemas and sets up a search server and an index,
  * with the specified processor enabled.
  *
  * @param string|null $processor
  *   (optional) The plugin ID of the processor that should be set up for
  *   testing.
  */
 public function setUp($processor = NULL)
 {
     parent::setUp();
     $this->installSchema('node', array('node_access'));
     $this->installSchema('search_api', array('search_api_item', 'search_api_task'));
     $this->installEntitySchema('user');
     $this->installEntitySchema('node');
     $this->installEntitySchema('comment');
     $this->installConfig(array('field'));
     Action::create(['id' => 'foo', 'label' => 'Foobaz', 'plugin' => 'comment_publish_action'])->save();
     \Drupal::configFactory()->getEditable('search_api.settings')->set('tracking_page_size', 100)->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);
     $this->server = Server::create(array('id' => 'server', 'name' => 'Server & Name', 'status' => TRUE, 'backend' => 'search_api_db', 'backend_config' => array('min_chars' => 3, 'database' => 'default:default')));
     $this->server->save();
     $this->index = Index::create(array('id' => 'index', 'name' => 'Index name', 'status' => TRUE, 'datasource_settings' => array('entity:comment' => array('plugin_id' => 'entity:comment', 'settings' => array()), 'entity:node' => array('plugin_id' => 'entity:node', 'settings' => array())), 'server' => 'server', 'tracker_settings' => array('default' => array('plugin_id' => 'default', 'settings' => array()))));
     $this->index->setServer($this->server);
     $field_subject = new Field($this->index, 'subject');
     $field_subject->setType('text');
     $field_subject->setPropertyPath('subject');
     $field_subject->setDatasourceId('entity:comment');
     $field_subject->setLabel('Subject');
     $field_title = new Field($this->index, 'title');
     $field_title->setType('text');
     $field_title->setPropertyPath('title');
     $field_title->setDatasourceId('entity:node');
     $field_title->setLabel('Title');
     $this->index->addField($field_subject);
     $this->index->addField($field_title);
     if ($processor) {
         /** @var \Drupal\search_api\Processor\ProcessorPluginManager $plugin_manager */
         $plugin_manager = \Drupal::service('plugin.manager.search_api.processor');
         $this->processor = $plugin_manager->createInstance($processor, array('index' => $this->index));
         $this->index->addProcessor($this->processor);
     }
     $this->index->save();
 }
Beispiel #9
0
 /**
  * Creates or deletes an index.
  *
  * @param string $name
  *   (optional) The name of the index.
  * @param string $id
  *   (optional) The ID of the index.
  * @param string $server_id
  *   (optional) The server to which the index should be attached.
  * @param string $datasource_id
  *   (optional) The ID of a datasource to set for this index.
  * @param bool $reset
  *   (optional) If TRUE, delete the index instead of creating it. (Only the
  *   index's ID is required in that case).
  *
  * @return \Drupal\search_api\IndexInterface
  *   A search index.
  */
 public function getTestIndex($name = 'WebTest Index', $id = 'webtest_index', $server_id = 'webtest_server', $datasource_id = 'entity:node', $reset = FALSE)
 {
     if ($reset) {
         $index = Index::load($id);
         if ($index) {
             $index->delete();
         }
     } else {
         $index = Index::create(array('id' => $id, 'name' => $name, 'description' => $name, 'server' => $server_id, 'datasources' => array($datasource_id)));
         $index->save();
         $this->indexId = $index->id();
     }
     return $index;
 }
Beispiel #10
0
  /**
   * {@inheritdoc}
   */
  public function removeIndex($index) {
    if (!is_object($index)) {
      // If the index got deleted, create a dummy to simplify the code. Since we
      // can't know, we assume the index was read-only, just to be on the safe
      // side.
      $index = Index::create(array(
        'id' => $index,
        'read_only' => TRUE,
      ));
    }

    $db_info = $this->getIndexDbInfo($index);

    try {
      if (!isset($db_info['field_tables']) && !isset($db_info['index_table'])) {
        return;
      }
      // Don't delete the index data of read-only indexes.
      if (!$index->isReadOnly()) {
        foreach ($db_info['field_tables'] as $field) {
          if ($this->database->schema()->tableExists($field['table'])) {
            $this->database->schema()->dropTable($field['table']);
          }
        }
        if ($this->database->schema()->tableExists($db_info['index_table'])) {
          $this->database->schema()->dropTable($db_info['index_table']);
        }
      }

      $this->getKeyValueStore()->delete($index->id());
    }
      // The database operations might throw PDO or other exceptions, so we catch
      // them all and re-wrap them appropriately.
    catch (\Exception $e) {
      throw new SearchApiException($e->getMessage(), $e->getCode(), $e);
    }
  }
 /**
  * {@inheritdoc}
  */
 public function removeIndex($index)
 {
     if (!is_object($index)) {
         // If the index got deleted, create a dummy to simplify the code. Since we
         // can't know, we assume the index was read-only, just to be on the safe
         // side.
         $index = Index::create(array('id' => $index, 'read_only' => TRUE));
     }
     try {
         if (!isset($this->configuration['field_tables'][$index->id()]) && !isset($this->configuration['index_tables'][$index->id()])) {
             return;
         }
         // Don't delete the index data of read-only indexes.
         if (!$index->isReadOnly()) {
             foreach ($this->configuration['field_tables'][$index->id()] as $field) {
                 if ($this->database->schema()->tableExists($field['table'])) {
                     $this->database->schema()->dropTable($field['table']);
                 }
             }
             if ($this->database->schema()->tableExists($this->configuration['index_tables'][$index->id()])) {
                 $this->database->schema()->dropTable($this->configuration['index_tables'][$index->id()]);
             }
         }
         unset($this->configuration['field_tables'][$index->id()]);
         unset($this->configuration['index_tables'][$index->id()]);
         $this->server->save();
     } catch (\Exception $e) {
         throw new SearchApiException($e->getMessage(), $e->getCode(), $e);
     }
 }
Beispiel #12
0
 /**
  * Creates or deletes an index.
  *
  * @param string $name
  *   (optional) The name of the index.
  * @param string $id
  *   (optional) The ID of the index.
  * @param string $server_id
  *   (optional) The server to which the index should be attached.
  * @param string $datasource_id
  *   (optional) The ID of a datasource to set for this index.
  * @param bool $reset
  *   (optional) If TRUE, delete the index instead of creating it. (Only the
  *   index's ID is required in that case.)
  *
  * @return \Drupal\search_api\IndexInterface
  *   A search index.
  */
 public function getTestIndex($name = 'WebTest Index', $id = 'webtest_index', $server_id = 'webtest_server', $datasource_id = 'entity:node', $reset = FALSE)
 {
     if ($reset) {
         /** @var \Drupal\search_api\IndexInterface $index */
         $index = Index::load($id);
         if ($index) {
             $index->delete();
         }
     } else {
         $index = Index::create(array('id' => $id, 'name' => $name, 'description' => $name . ' description', 'server' => $server_id, 'datasource_settings' => array($datasource_id => array('plugin_id' => $datasource_id, 'settings' => array()))));
         $index->save();
         $this->indexId = $index->id();
     }
     return $index;
 }