You are here

public function ResponsivePreviewToolbarTest::testCacheInvalidation in Responsive Theme Preview 8

Tests cache invalidation.

File

tests/src/Functional/ResponsivePreviewToolbarTest.php, line 99

Class

ResponsivePreviewToolbarTest
Tests the toolbar integration.

Namespace

Drupal\Tests\responsive_preview\Functional

Code

public function testCacheInvalidation() {
  $device_storage = \Drupal::entityTypeManager()
    ->getStorage('responsive_preview_device');
  $device_ids = array_keys($this
    ->getDefaultDevices(TRUE));
  $devices = array_combine($device_ids, $device_ids);
  $this
    ->drupalLogin($this->previewUser);

  // Initially only the default enabled devices should appear in the list.
  $this
    ->drupalGet('');
  $this
    ->assertDeviceListEquals($devices);

  // Update a device should invalidate the cache, so you should get
  // the updated device list.
  $devices['large'] = 'large';
  $device_storage
    ->load('large')
    ->setStatus(1)
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertDeviceListEquals($devices);

  // Add a device should invalidate the cache, so you should get
  // the updated device list.
  $devices['new_device'] = 'new_device';
  $device_storage
    ->create([
    'id' => 'new_device',
    'label' => 'Hello, I am new!',
    'status' => '1',
    'orientation' => 'landscape',
    'dimensions' => [
      'width' => '1600',
      'height' => '2850',
      'dppx' => '2.5',
    ],
  ])
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertDeviceListEquals($devices);

  // Delete a device should invalidate the cache, so you should get
  // the updated device list.
  unset($devices['large'], $devices['new_device']);
  $entities = $device_storage
    ->loadMultiple([
    'new_device',
    'large',
  ]);
  $device_storage
    ->delete($entities);
  $this
    ->drupalGet('');
  $this
    ->assertDeviceListEquals($devices);
}