View source
<?php
namespace Drupal\Tests\search_api_solr_defaults\Functional;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\ServerInterface;
use Drupal\search_api_solr\Utility\SolrCommitTrait;
use Drupal\Tests\BrowserTestBase;
class IntegrationTest extends BrowserTestBase {
use SolrCommitTrait;
protected $profile = 'standard';
protected $authenticatedUser;
protected $adminUser;
public function setUp() {
parent::setUp();
$this->authenticatedUser = $this
->drupalCreateUser();
$this->adminUser = $this
->drupalCreateUser([], NULL, TRUE);
}
public function testInstallAndDefaultSetupWorking() {
$this
->drupalLogin($this->adminUser);
\Drupal::state()
->set('search_api_use_tracking_batch', FALSE);
$edit_enable = [
'modules[search_api_solr_defaults][enable]' => TRUE,
];
$this
->drupalPostForm('admin/modules', $edit_enable, 'Install');
$this
->assertSession()
->responseContains('Some required modules must be enabled');
$this
->drupalPostForm(NULL, [], 'Continue');
$this
->assertSession()
->responseContains('modules have been enabled');
$this
->rebuildContainer();
$this
->resetAll();
$this
->drupalPostForm('admin/config/search/search-api/server/default_solr_server/edit', [], 'Save');
$this
->assertSession()
->pageTextContains('The server was successfully saved.');
$server = Server::load('default_solr_server');
$this
->assertInstanceOf(ServerInterface::class, $server, 'Server can be loaded');
$index = Index::load('default_solr_index');
$this
->assertInstanceOf(IndexInterface::class, $index, 'Index can be loaded');
$this
->drupalLogin($this->authenticatedUser);
$this
->drupalGet('solr-search/content');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$title = 'Test node title';
$edit = [
'title[0][value]' => $title,
'body[0][value]' => 'This is test content for the Search API to index.',
];
$this
->drupalPostForm('node/add/article', $edit, 'Save');
$this
->ensureCommit($index);
$this
->drupalLogout();
$this
->drupalGet('solr-search/content');
$this
->assertSession()
->pageTextContains('Please enter some keywords to search.');
$this
->assertSession()
->pageTextNotContains($title);
$this
->assertSession()
->responseNotContains('Error message');
$this
->submitForm([], 'Search');
$this
->assertSession()
->pageTextNotContains($title);
$this
->assertSession()
->responseNotContains('Error message');
$this
->submitForm([
'keys' => 'test',
], 'Search');
$this
->assertSession()
->pageTextContains($title);
$this
->assertSession()
->responseNotContains('Error message');
$this
->drupalLogin($this->adminUser);
$edit_disable = [
'uninstall[search_api_solr_defaults]' => TRUE,
];
$this
->drupalPostForm('admin/modules/uninstall', $edit_disable, 'Uninstall');
$this
->submitForm([], 'Uninstall');
$this
->rebuildContainer();
$this
->assertFalse($this->container
->get('module_handler')
->moduleExists('search_api_solr_defaults'), 'Solr Search Defaults module uninstalled.');
$this
->drupalGet('admin/config/search/search-api/server/default_solr_server');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/config/search/search-api/index/default_solr_index');
$this
->assertSession()
->statusCodeEquals(200);
foreach ([
'edit',
'fields',
'processors',
] as $tab) {
$submit = $tab === 'fields' ? 'Save changes' : 'Save';
$this
->drupalGet("admin/config/search/search-api/index/default_solr_index/{$tab}");
$this
->submitForm([], $submit);
$this
->assertSession()
->statusCodeEquals(200);
}
$this
->drupalLogin($this->authenticatedUser);
$this
->drupalGet('solr-search/content');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm('admin/modules', $edit_enable, 'Install');
$this
->assertSession()
->pageTextContains('It looks like the default setup provided by this module already exists on your site. Cannot re-install module.');
$entities_to_remove = [
'search_api_index' => 'default_solr_index',
'search_api_server' => 'default_solr_server',
'view' => 'solr_search_content',
];
$entity_type_manager = \Drupal::service('entity_type.manager');
foreach ($entities_to_remove as $entity_type => $entity_id) {
$entity_storage = $entity_type_manager
->getStorage($entity_type);
$entity_storage
->resetCache();
$entities = $entity_storage
->loadByProperties([
'id' => $entity_id,
]);
if (!empty($entities[$entity_id])) {
$entities[$entity_id]
->delete();
}
}
$this
->drupalGet('node/1/delete');
$this
->submitForm([], 'Delete');
$this
->drupalGet('admin/structure/types/manage/article');
$this
->clickLink('Delete');
$this
->assertSession()
->statusCodeEquals(200);
$this
->submitForm([], 'Delete');
$this
->drupalPostForm('admin/modules', $edit_enable, 'Install');
$success_text = t('Content type @content_type not found. Solr Search Defaults module could not be installed.', [
'@content_type' => 'article',
]);
$this
->assertSession()
->pageTextContains($success_text);
}
}