コード例 #1
0
 /**
  * Tests a backend with a dependency that gets removed.
  *
  * If the dependency does not get removed, proper cascading to the index is
  * also verified.
  *
  * @param bool $remove_dependency
  *   Whether to remove the dependency from the backend when the object
  *   depended on is deleted.
  *
  * @dataProvider dependencyTestDataProvider
  */
 public function testBackendDependency($remove_dependency)
 {
     $dependency_key = $this->dependency->getConfigDependencyKey();
     $dependency_name = $this->dependency->getConfigDependencyName();
     // Create a server using the test backend, and set the dependency in the
     // configuration.
     /** @var \Drupal\search_api\ServerInterface $server */
     $server = Server::create(array('id' => 'test_server', 'name' => 'Test server', 'backend' => 'search_api_test_backend', 'backend_config' => array('dependencies' => array($dependency_key => array($dependency_name)))));
     $server->save();
     $server_dependency_key = $server->getConfigDependencyKey();
     $server_dependency_name = $server->getConfigDependencyName();
     // Set the server on the index and save that, too. However, we don't want
     // the index enabled, since that would lead to all kinds of overhead which
     // is completely irrelevant for this test.
     $this->index->setServer($server);
     $this->index->disable();
     $this->index->save();
     // Check that the dependencies were calculated correctly.
     $server_dependencies = $server->getDependencies();
     $this->assertContains($dependency_name, $server_dependencies[$dependency_key], 'Backend dependency correctly inserted');
     $index_dependencies = $this->index->getDependencies();
     $this->assertContains($server_dependency_name, $index_dependencies[$server_dependency_key], 'Server dependency correctly inserted');
     // Set our magic state key to let the test plugin know whether the
     // dependency should be removed or not. See
     // \Drupal\search_api_test_backend\Plugin\search_api\backend\TestBackend::onDependencyRemoval().
     $key = 'search_api_test_backend.dependencies.remove';
     \Drupal::state()->set($key, $remove_dependency);
     // Delete the backend's dependency.
     $this->dependency->delete();
     // Reload the index and check it's still there.
     $this->reloadIndex();
     $this->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed');
     // Reload the server.
     $storage = \Drupal::entityTypeManager()->getStorage('search_api_server');
     $storage->resetCache();
     $server = $storage->load($server->id());
     if ($remove_dependency) {
         $this->assertInstanceOf('Drupal\\search_api\\ServerInterface', $server, 'Server was not removed');
         $this->assertArrayNotHasKey('dependencies', $server->get('backend_config'), 'Backend config was adapted');
         // @todo Logically, this should not be changed: if the server does not get
         //   removed, there is no need to adapt the index's configuration.
         //   However, the way this config dependency cascading is actually
         //   implemented in
         //   \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval()
         //   does not seem to follow that logic, but just computes the complete
         //   tree of dependencies once and operates generally on the assumption
         //   that all of them will be deleted. See #2642374.
         //      $this->assertEquals($server->id(), $this->index->getServerId(), "Index's server was not changed");
     } else {
         $this->assertNull($server, 'Server was removed');
         $this->assertEquals(NULL, $this->index->getServerId(), 'Index server was changed');
     }
 }
コード例 #2
0
ファイル: Index.php プロジェクト: curveagency/intranet
 /**
  * Checks whether the index switched server 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 reactToServerSwitch(IndexInterface $original)
 {
     // Asserts that the index was enabled before saving and will still be
     // enabled afterwards. Otherwise, this method should not be called.
     assert('$this->status() && $original->status()', '::reactToServerSwitch should only be called when the index is enabled');
     if ($this->getServerId() != $original->getServerId()) {
         if ($original->isServerEnabled()) {
             $original->getServerInstance()->removeIndex($this);
         }
         if ($this->isServerEnabled()) {
             $this->getServerInstance()->addIndex($this);
         }
         // When the server changes we also need to trigger a reindex.
         $this->reindex();
     } elseif ($this->isServerEnabled()) {
         // Tell the server the index configuration got updated
         $this->getServerInstance()->updateIndex($this);
     }
 }
コード例 #3
0
ファイル: Index.php プロジェクト: jkyto/agolf
 /**
  * Checks whether the index switched server 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 reactToServerSwitch(IndexInterface $original) {
   if ($this->getServerId() != $original->getServerId()) {
     if ($original->isServerEnabled()) {
       $original->getServer()->removeIndex($this);
     }
     if ($this->isServerEnabled()) {
       $this->getServer()->addIndex($this);
     }
     // When the server changes we also need to trigger a reindex.
     $this->reindex();
   }
   elseif ($this->isServerEnabled()) {
     // Tell the server the index configuration got updated
     $this->getServer()->updateIndex($this);
   }
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  */
 public function getServerId()
 {
     return $this->entity->getServerId();
 }