SpambotCacheTest.php in Spambot 8
File
tests/src/Kernel/SpambotCacheTest.php
View source
<?php
namespace Drupal\Tests\spambot\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
use Drupal\KernelTests\KernelTestBase;
class SpambotCacheTest extends KernelTestBase implements ServiceModifierInterface {
protected static $modules = [
'spambot',
];
public function setUp() : void {
parent::setUp();
$this
->installConfig([
'spambot',
]);
}
public function testSpambotCaching() {
$username = mb_strtolower($this
->getRandomGenerator()
->name());
$email = mb_strtolower($this
->getRandomGenerator()
->name()) . '@example.com';
$ip = '' . mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255);
$mock_query = [
'username' => $username,
'email' => $email,
'ip' => $ip,
];
$mock_data = [];
spambot_sfs_request($mock_query, $mock_data);
$cache_username = \Drupal::cache('spambot')
->get("username:{$username}");
$cache_email = \Drupal::cache('spambot')
->get("email:{$email}");
$cache_ip = \Drupal::cache('spambot')
->get("ip:{$ip}");
$this
->assertNotFalse($cache_username);
$this
->assertNotFalse($cache_email);
$this
->assertNotFalse($cache_ip);
}
public function alter(ContainerBuilder $container) {
$container
->removeDefinition('test.http_client.middleware');
}
}