scanner-8.x-1.0-rc3/tests/src/Functional/SearchReplaceTest.php
tests/src/Functional/SearchReplaceTest.php
<?php
namespace Drupal\Tests\scanner\Functional;
use Drupal\Core\Session\AccountInterface;
/**
* Tests the search and replace functionality.
*
* @group scanner
*/
class SearchReplaceTest extends ScannerTestBase {
/**
* Admin user 1.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected AccountInterface $user1;
/**
* Ordinary user 2.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected AccountInterface $user2;
/**
* {@inheritdoc}
*
* @throws \Behat\Mink\Exception\ExpectationException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function setUp(): void {
// Make sure to complete the normal setup steps first.
parent::setUp();
// Create a test content types.
$this->createContentTypeNode('Title test', 'Body test', 'scanner_test_node_type', 'Scanner test node type');
// Log in as an admin who can modify the module's settings.
$user_1 = $this->createUser(['administer scanner settings']);
$this->drupalLogin($user_1);
$this->user1 = $user_1;
// Enable the content type.
$this->drupalGet('admin/config/content/scanner');
$this->assertSession()->statusCodeEquals(200);
$edit = [
'enabled_content_types[node:scanner_test_node_type]' => 'node:scanner_test_node_type',
];
$this->submitForm($edit, 'Save configuration');
$edit = [
'fields_of_selected_content_type[node:scanner_test_node_type:body]' => 'node:scanner_test_node_type:body',
'fields_of_selected_content_type[node:scanner_test_node_type:link_field]' => 'node:scanner_test_node_type:link_field',
];
$this->submitForm($edit, 'Save configuration');
$this->assertSession()->statusCodeEquals(200);
// Log in as a user that can use the "replace" system.
$user_2 = $this->createUser([
// 'administer nodes',
'perform search only',
'perform search and replace',
]);
$this->drupalLogin($user_2);
$this->user2 = $user_2;
}
/**
* Test the search form.
*
* @throws \Behat\Mink\Exception\ExpectationException
*/
public function testSearchReplaceForm(): void {
// Load the main scanner form.
$this->drupalGet('admin/content/scanner');
$session_assert = $this->assertSession();
$session_assert->statusCodeEquals(200);
// Verify the form has the expected fields.
$session_assert->fieldExists('search');
$session_assert->fieldExists('replace');
$session_assert->fieldExists('preceded');
$session_assert->fieldExists('followed');
$session_assert->fieldExists('mode');
$session_assert->fieldExists('wholeword');
$session_assert->fieldExists('regex');
$session_assert->fieldExists('published');
$session_assert->fieldExists('language');
}
/**
* Test the complete search & replace operation in one go.
*
* @throws \Behat\Mink\Exception\ResponseTextException
* @throws \Behat\Mink\Exception\ExpectationException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function testSearchReplaceBody(): void {
$this->searchAndReplace('Body test', 'scanner');
}
/**
* Test the complete search & replace operation for link fields.
*
* @throws \Behat\Mink\Exception\ResponseTextException
* @throws \Behat\Mink\Exception\ExpectationException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function testSearchReplaceLink(): void {
$this->searchAndReplace('https://example.com', 'https://exampleNew.com');
}
/**
* Re-usable function test search and replace.
*
* @param string $search
* Text to search for.
* @param string $replace
* Text to replace search text with.
*
* @throws \Behat\Mink\Exception\ResponseTextException
* @throws \Behat\Mink\Exception\ExpectationException
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
private function searchAndReplace(string $search, string $replace): void {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($search);
// Node belongs to anonymous user.
$this->assertEquals('0', $node_storage->loadUnchanged(1)->getRevisionUserId());
$this->drupalGet('admin/content/scanner');
// Test the search operation.
$this->submitForm(['search' => $search], 'Search');
$this->assertSession()->statusCodeEquals(200);
// Make sure no errors were reported.
$this->assertSession()->pageTextNotContains('An error has occurred.');
$this->assertSession()->pageTextNotContains('Found 0 matches in 0 entities.');
$this->assertSession()->pageTextContains('Found 1 matches in 1 entities.');
$this->submitForm(['search' => 'scanner'], 'Search');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextNotContains('Found 1 matches in 1 entities.');
$this->assertSession()->pageTextContains('Found 0 matches in 0 entities.');
// Test the replacement operation.
$this->submitForm(['search' => $search, 'replace' => $replace], 'Replace');
$this->assertSession()->statusCodeEquals(200);
$this->submitForm([], 'Confirm');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('1 entities processed.');
// After search-replace, node revision author is user that done
// the search-replace.
$this->assertEquals($this->user2->id(), $node_storage->loadUnchanged(1)->getRevisionUserId());
// Verify that the string changed.
$this->submitForm(['search' => $search], 'Search');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Found 0 matches in 0 entities.');
$this->submitForm(['search' => $replace], 'Search');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Found 1 matches in 1 entities.');
// Verify that the node's text has changed.
$this->drupalGet('node/1');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextNotContains($search);
$this->assertSession()->pageTextContains($replace);
}
}
