View source
<?php
namespace Drupal\Tests\search_api_db_defaults\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\Tests\BrowserTestBase;
class IntegrationTest extends BrowserTestBase {
use StringTranslationTrait, CommentTestTrait, EntityReferenceTestTrait;
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_db_defaults][enable]' => TRUE,
];
$this
->drupalGet('admin/modules');
$this
->submitForm($edit_enable, 'Install');
$this
->assertSession()
->pageTextContains('Some required modules must be enabled');
$this
->submitForm([], 'Continue');
$this
->assertSession()
->pageTextContains('3 modules have been enabled: Database Search Defaults, Database Search, Search API');
$this
->rebuildContainer();
$this
->drupalGet('admin/config/search/search-api/server/default_server/edit');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('The server was successfully saved.');
$server = Server::load('default_server');
$this
->assertInstanceOf(Server::class, $server, 'Server can be loaded');
$index = Index::load('default_index');
$this
->assertInstanceOf(Index::class, $index, 'Index can be loaded');
$this
->drupalLogin($this->authenticatedUser);
$this
->drupalGet('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
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$this
->drupalLogout();
$this
->drupalGet('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_db_defaults]' => TRUE,
];
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit_disable, 'Uninstall');
$this
->submitForm([], 'Uninstall');
$this
->rebuildContainer();
$this
->assertFalse($this->container
->get('module_handler')
->moduleExists('search_api_db_defaults'), 'Search API DB Defaults module uninstalled.');
$this
->drupalGet('admin/config/search/search-api/server/default_server');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/config/search/search-api/index/default_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_index/{$tab}");
$this
->submitForm([], $submit);
$this
->assertSession()
->statusCodeEquals(200);
}
$this
->drupalLogin($this->authenticatedUser);
$this
->drupalGet('search/content');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/modules');
$this
->submitForm($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_index',
'search_api_server' => 'default_server',
'view' => '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
->drupalGet('admin/modules');
$this
->submitForm($edit_enable, 'Install');
$success_text = new FormattableMarkup('Content type @content_type not found. Database Search Defaults module could not be installed.', [
'@content_type' => 'article',
]);
$this
->assertSession()
->pageTextContains($success_text);
}
}