1<?php
2
3namespace Drupal\Tests\views_ui\Functional;
4
5/**
6 * Tests the shared tempstore cache in the UI.
7 *
8 * @group views_ui
9 */
10class CachedDataUITest extends UITestBase {
11
12  /**
13   * Views used by this test.
14   *
15   * @var array
16   */
17  public static $testViews = ['test_view'];
18
19  /**
20   * {@inheritdoc}
21   */
22  protected $defaultTheme = 'stark';
23
24  /**
25   * Tests the shared tempstore views data in the UI.
26   */
27  public function testCacheData() {
28    $views_admin_user_uid = $this->fullAdminUser->id();
29
30    $temp_store = $this->container->get('tempstore.shared')->get('views');
31    // The view should not be locked.
32    $this->assertEqual($temp_store->getMetadata('test_view'), NULL, 'The view is not locked.');
33
34    $this->drupalGet('admin/structure/views/view/test_view/edit');
35    // Make sure we have 'changes' to the view.
36    $this->drupalPostForm('admin/structure/views/nojs/display/test_view/default/title', [], t('Apply'));
37    $this->assertText('You have unsaved changes.');
38    $this->assertEqual($temp_store->getMetadata('test_view')->getOwnerId(), $views_admin_user_uid, 'View cache has been saved.');
39
40    $view_cache = $temp_store->get('test_view');
41    // The view should be enabled.
42    $this->assertTrue($view_cache->status(), 'The view is enabled.');
43    // The view should now be locked.
44    $this->assertEqual($temp_store->getMetadata('test_view')->getOwnerId(), $views_admin_user_uid, 'The view is locked.');
45
46    // Cancel the view edit and make sure the cache is deleted.
47    $this->drupalPostForm(NULL, [], t('Cancel'));
48    $this->assertEqual($temp_store->getMetadata('test_view'), NULL, 'Shared tempstore data has been removed.');
49    // Test we are redirected to the view listing page.
50    $this->assertUrl('admin/structure/views', [], 'Redirected back to the view listing page.');
51
52    // Log in with another user and make sure the view is locked and break.
53    $this->drupalPostForm('admin/structure/views/nojs/display/test_view/default/title', [], t('Apply'));
54    $this->drupalLogin($this->adminUser);
55
56    $this->drupalGet('admin/structure/views/view/test_view/edit');
57    // Test that save and cancel buttons are not shown.
58    $this->assertNoFieldById('edit-actions-submit', t('Save'));
59    $this->assertNoFieldById('edit-actions-cancel', t('Cancel'));
60    // Test we have the break lock link.
61    $this->assertLinkByHref('admin/structure/views/view/test_view/break-lock');
62    // Break the lock.
63    $this->clickLink(t('break this lock'));
64    $this->drupalPostForm(NULL, [], t('Break lock'));
65    // Test that save and cancel buttons are shown.
66    $this->assertFieldById('edit-actions-submit', t('Save'));
67    $this->assertFieldById('edit-actions-cancel', t('Cancel'));
68    // Test we can save the view.
69    $this->drupalPostForm('admin/structure/views/view/test_view/edit', [], t('Save'));
70    $this->assertRaw(t('The view %view has been saved.', ['%view' => 'Test view']));
71
72    // Test that a deleted view has no tempstore data.
73    $this->drupalPostForm('admin/structure/views/nojs/display/test_view/default/title', [], t('Apply'));
74    $this->drupalPostForm('admin/structure/views/view/test_view/delete', [], t('Delete'));
75    // No view tempstore data should be returned for this view after deletion.
76    $this->assertEqual($temp_store->getMetadata('test_view'), NULL, 'View tempstore data has been removed after deletion.');
77  }
78
79}
80