CacheFlushTest.php in CacheFlush 8
File
tests/src/Functional/CacheFlushTest.php
View source
<?php
namespace Drupal\Tests\cacheflush\Functional;
use Drupal\cacheflush\Controller\CacheflushApi;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CacheFlushTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected $testUser;
public static $modules = [
'cacheflush',
];
protected $container;
public function __construct(ContainerInterface $container) {
parent::__construct();
$this->container = $container;
}
protected function setUp() {
parent::setUp();
$this->testUser = $this
->drupalCreateUser([
'cacheflush clear cache',
]);
}
public function testMenu() {
$this
->menuAccessAnonymusUser();
$this
->clearPresetMenu();
}
public function menuAccessAnonymusUser() {
$this
->drupalGet('admin/cacheflush');
$this
->assertResponse(403);
$this
->drupalGet('admin/cacheflush/clear/all');
$this
->assertResponse(403);
$this
->drupalGet('admin/cacheflush/clear/1');
$this
->assertResponse(404);
}
public function clearPresetMenu() {
$this
->createTestEntitys();
$enabled = array_values(cacheflush_load_multiple_by_properties([
'title' => 'Enabled',
'status' => 1,
]));
$this
->assertEqual($enabled[0]->title->value, 'Enabled', 'Created and loaded entity: enabled.');
$disabled = array_values(cacheflush_load_multiple_by_properties([
'title' => 'Disabled',
'status' => 0,
]));
$this
->assertEqual($disabled[0]->title->value, 'Disabled', 'Created and loaded entity: disabled.');
$this
->drupalLogin($this->testUser);
$this
->drupalGet('admin/cacheflush');
$this
->assertResponse(200);
$this
->drupalGet('admin/cacheflush/clear/all');
$this
->assertResponse(200);
$this
->drupalGet('admin/cacheflush/clear/' . $enabled[0]->id->value);
$this
->assertResponse(200);
$this
->drupalGet('admin/cacheflush/clear/' . $disabled[0]->id->value);
$this
->assertResponse(403);
$this
->drupalLogout();
}
public function createTestEntitys() {
$data = [];
foreach (CacheflushApi::create($this->container)
->getOptionList() as $key => $value) {
$data[$key]['functions'] = $value['functions'];
}
$data = serialize($data);
$entity = cacheflush_create([
'title' => 'Enabled',
'status' => 1,
'data' => $data,
]);
$entity
->save();
$entity = cacheflush_create([
'title' => 'Disabled',
'status' => 0,
'data' => $data,
]);
$entity
->save();
}
}