View source  
  <?php
namespace Drupal\Tests\cacheflush_ui\Functional;
use Drupal\cacheflush\Controller\CacheflushApi;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CacheFlushUICRUD extends BrowserTestBase {
  
  protected $defaultTheme = 'stark';
  
  public static $modules = [
    'cacheflush_ui',
  ];
  
  public static $roles = [
    'cacheflush create new',
    'cacheflush administer',
    'cacheflush view own',
    'cacheflush edit own',
    'cacheflush delete own',
  ];
  
  protected $moduleHandler;
  
  protected $entityTypeManager;
  
  protected $container;
  
  public function __construct(ModuleHandler $module_handler, EntityTypeManagerInterface $entity_type_manager, ContainerInterface $container) {
    parent::__construct();
    $this->moduleHandler = $module_handler;
    $this->entityTypeManager = $entity_type_manager;
    $this->container = $container;
  }
  
  public function setUp() {
    parent::setUp();
    $user = $this
      ->drupalCreateUser(self::$roles);
    $this
      ->drupalLogin($user);
  }
  
  public function testCrud() {
    $this
      ->addInterfaceIntegrity();
    $this
      ->fieldRequiring();
    $this
      ->crudTest();
  }
  
  public function addInterfaceIntegrity() {
    $this
      ->drupalGet('admin/structure/cacheflush/add');
    $this
      ->assertResponse(200);
    $this
      ->assertFieldByName('title');
    $this
      ->assertFieldByName('op', t('Save'));
    $tabs = $this->moduleHandler
      ->invokeAll('cacheflush_ui_tabs');
    if ($tabs) {
      foreach ($tabs as $key => $value) {
        $this
          ->assertText($value['name']);
      }
    }
    $checkboxes = CacheflushApi::create($this->container)
      ->getOptionList();
    if ($checkboxes) {
      foreach ($checkboxes as $key => $value) {
        $this
          ->assertFieldByName($value['category'] . "[{$key}]");
      }
    }
  }
  
  public function fieldRequiring() {
    $this
      ->drupalPostForm('admin/structure/cacheflush/add', [], t('Save'));
    $this
      ->assertResponse(200);
    $this
      ->assertRaw('error');
  }
  
  public function crudTest() {
    
    $data = [
      'title' => 'NewEntityTitle',
      'vertical_tabs_core[bootstrap]' => 1,
      'vertical_tabs_core[config]' => 1,
    ];
    $this
      ->drupalPostForm('admin/structure/cacheflush/add', $data, t('Save'));
    $entities = array_values(cacheflush_load_multiple_by_properties([
      'title' => 'NewEntityTitle',
    ]));
    $this
      ->assertEqual($entities[0]
      ->getTitle(), 'NewEntityTitle', 'Entity successfully created.');
    
    $data2 = [
      'title' => 'UpdatedEntityTitle',
      'vertical_tabs_core[default]' => 1,
      'vertical_tabs_core[config]' => FALSE,
    ];
    $this
      ->drupalGet('cacheflush/' . $entities[0]
      ->id() . '/edit');
    $this
      ->assertFieldByName('vertical_tabs_core[bootstrap]', 'Entity 1: vertical_tabs_core[bootstrap] value appears correctly in the form.');
    $this
      ->assertFieldByName('vertical_tabs_core[config]', 'Entity 1: vertical_tabs_core[config] value appears correctly in the form.');
    $this
      ->assertFieldByName('vertical_tabs_core[default]', 'Entity 1: vertical_tabs_core[default] value appears correctly in the form.');
    $this
      ->drupalPostForm('cacheflush/' . $entities[0]
      ->id() . '/edit', $data2, t('Save'));
    $this->entityTypeManager
      ->getStorage('cacheflush')
      ->resetCache([
      $entities[0]
        ->id(),
    ]);
    $entities = array_values(cacheflush_load_multiple_by_properties([
      'title' => 'UpdatedEntityTitle',
    ]));
    $this
      ->assertEqual($entities[0]
      ->getTitle(), 'UpdatedEntityTitle', 'Entity successfully updated.');
    $this
      ->drupalGet('cacheflush/' . $entities[0]
      ->id() . '/edit');
    $this
      ->assertFieldByName('vertical_tabs_core[bootstrap]', 'Entity 1: vertical_tabs_core[bootstrap] value appears correctly in the form.');
    $this
      ->assertFieldByName('vertical_tabs_core[config]', 'Entity 1: vertical_tabs_core[config] value appears correctly in the form.');
    $this
      ->assertFieldByName('vertical_tabs_core[default]', 'Entity 1: vertical_tabs_core[default] value appears correctly in the form.');
    
    $this
      ->drupalGet('cacheflush/' . $entities[0]
      ->id() . '/delete');
    $this
      ->assertLink(t('Cancel'));
    $this
      ->assertFieldByName('op', t('Delete'));
    $this
      ->drupalPostForm(NULL, [], t('Delete'));
    $this
      ->drupalGet('cacheflush/' . $entities[0]
      ->id());
    $this
      ->assertResponse(404);
  }
}