/**
   * {@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);
  }
Beispiel #2
0
  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->setUpExampleStructure();

    Utility::getIndexTaskManager()->addItemsAll(Index::load($this->indexId));
  }
Beispiel #3
0
  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->installSchema('search_api', array('search_api_item', 'search_api_task'));
    $this->installSchema('system', array('router'));
    $this->installSchema('user', array('users_data'));

    $this->setUpExampleStructure();

    $this->installConfig(array('search_api_test_db'));

    Utility::getIndexTaskManager()->addItemsAll($this->getIndex());
  }
Beispiel #4
0
 /**
  * Checks whether the index switched tracker plugin and reacts accordingly.
  *
  * Used as a helper method in postSave(). Should only be called when the index
  * was enabled before the change and remained so.
  *
  * @param \Drupal\search_api\IndexInterface $original
  *   The previous version of the index.
  */
 protected function reactToTrackerSwitch(IndexInterface $original) {
   if ($this->tracker != $original->getTrackerId()) {
     $index_task_manager = Utility::getIndexTaskManager();
     if ($original->hasValidTracker()) {
       $index_task_manager->stopTracking($this);
     }
     if ($this->hasValidTracker()) {
       $index_task_manager->startTracking($this);
     }
   }
 }
Beispiel #5
0
  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    // @todo Redirect to a confirm form if changing server or tracker, since
    //   that isn't such a light operation (equaling a "clear", basically).

    // Only save the index if the form doesn't need to be rebuilt.
    if (!$form_state->isRebuilding()) {
      try {
        /** @var \Drupal\search_api\IndexInterface $index */
        $index = $this->getEntity();
        $index->save();
        drupal_set_message($this->t('The index was successfully saved.'));
        $form_state->setRedirect('entity.search_api_index.canonical', array('search_api_index' => $index->id()));
        $index_task_manager = Utility::getIndexTaskManager();
        if (!$index_task_manager->isTrackingComplete($index)) {
          $index_task_manager->addItemsBatch($index);
        }
      }
      catch (SearchApiException $ex) {
        $form_state->setRebuild();
        watchdog_exception('search_api', $ex);
        drupal_set_message($this->t('The index could not be saved.'), 'error');
      }
    }
  }
Beispiel #6
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);
  }
Beispiel #7
0
  /**
   * Performs setup tasks before each individual test method is run.
   */
  public function setUp() {
    parent::setUp('content_access');

    // The parent method already installs most needed node and comment schemas,
    // but here we also need the comment statistics.
    $this->installSchema('comment', array('comment_entity_statistics'));

    // Create a node type for testing.
    $type = NodeType::create(array('type' => 'page', 'name' => 'page'));
    $type->save();

    // Create anonymous user role.
    $role = Role::create(array(
      'id' => 'anonymous',
      'label' => 'anonymous',
    ));
    $role->save();

    // Insert the anonymous user into the database, as the user table is inner
    // joined by \Drupal\comment\CommentStorage.
    User::create(array(
      'uid' => 0,
      'name' => '',
    ))->save();

    // Create a node with attached comment.
    $this->nodes[0] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'test title'));
    $this->nodes[0]->save();

    $comment_type = CommentType::create(array(
      'id' => 'comment',
      'target_entity_type_id' => 'node',
    ));
    $comment_type->save();

    $this->installConfig(array('comment'));
    $this->addDefaultCommentField('node', 'page');

    $comment = Comment::create(array(
      'entity_type' => 'node',
      'entity_id' => $this->nodes[0]->id(),
      'field_name' => 'comment',
      'body' => 'test body',
      'comment_type' => $comment_type->id(),
    ));
    $comment->save();

    $this->comments[] = $comment;

    $this->nodes[1] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'some title'));
    $this->nodes[1]->save();

    $this->nodes[2] = Node::create(array('status' => NODE_NOT_PUBLISHED, 'type' => 'page', 'title' => 'other title'));
    $this->nodes[2]->save();

    // Also index users, to verify that they are unaffected by the processor.
    $this->index->set('datasources', array('entity:comment', 'entity:node', 'entity:user'));
    $this->index->save();

    Utility::getIndexTaskManager()->addItemsAll($this->index);
    $index_storage = \Drupal::entityManager()->getStorage('search_api_index');
    $index_storage->resetCache([$this->index->id()]);
    $this->index = $index_storage->load($this->index->id());
  }
Beispiel #8
0
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $form['#index'];

    switch ($form_state->getTriggeringElement()['#name']) {
      case 'index_now':
        $values = $form_state->getValues();
        try {
          IndexBatchHelper::setStringTranslation($this->getStringTranslation());
          IndexBatchHelper::create($index, $values['batch_size'], $values['limit']);
        }
        catch (SearchApiException $e) {
          drupal_set_message($this->t('Failed to create a batch, please check the batch size and limit.'), 'warning');
        }
        break;

      case 'reindex':
        $form_state->setRedirect('entity.search_api_index.reindex', array('search_api_index' => $index->id()));
        break;

      case 'clear':
        $form_state->setRedirect('entity.search_api_index.clear', array('search_api_index' => $index->id()));
        break;

      case 'track_now':
        Utility::getIndexTaskManager()->addItemsBatch($index);
        break;
    }
  }
Beispiel #9
0
 /**
  * Private method to setup Search API database and server.
  */
 private function setupSearchAPI()
 {
     $server = array('name' => $this->serverId, 'id' => $this->serverId, 'backend' => 'search_api_db');
     $this->drupalPostForm('admin/config/search/search-api/add-server', $server, 'Save');
     // The notice will be fixed when https://www.drupal.org/node/1299238 is in.
     $index = array('name' => $this->indexId, 'id' => $this->indexId, 'datasources[]' => 'entity:node', 'server' => $this->serverId);
     $this->drupalPostForm('admin/config/search/search-api/add-index', $index, 'Save');
     $processors = array('status[rendered_item]' => TRUE);
     $this->drupalPostForm('admin/config/search/search-api/index/' . $this->indexId . '/processors', $processors, 'Save');
     Utility::getIndexTaskManager()->addItemsAll(Index::load($this->indexId));
     $this->indexItems($this->indexId);
 }