Backup of db before drupal security update
[yaffs-website] / web / core / modules / system / tests / src / Functional / Lock / LockFunctionalTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Lock;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Confirm locking works between two separate requests.
9  *
10  * @group Lock
11  */
12 class LockFunctionalTest extends BrowserTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['system_test'];
20
21   /**
22    * Confirms that we can acquire and release locks in two parallel requests.
23    */
24   public function testLockAcquire() {
25     $lock = $this->container->get('lock');
26     $lock_acquired = 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockAcquire()';
27     $lock_not_acquired = 'FALSE: Lock not acquired in \Drupal\system_test\Controller\SystemTestController::lockAcquire()';
28     $this->assertTrue($lock->acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock');
29     $this->assertTrue($lock->acquire('system_test_lock_acquire'), 'Lock extended by this request.', 'Lock');
30     $lock->release('system_test_lock_acquire');
31
32     // Cause another request to acquire the lock.
33     $this->drupalGet('system-test/lock-acquire');
34     $this->assertText($lock_acquired, 'Lock acquired by the other request.', 'Lock');
35     // The other request has finished, thus it should have released its lock.
36     $this->assertTrue($lock->acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock');
37     // This request holds the lock, so the other request cannot acquire it.
38     $this->drupalGet('system-test/lock-acquire');
39     $this->assertText($lock_not_acquired, 'Lock not acquired by the other request.', 'Lock');
40     $lock->release('system_test_lock_acquire');
41
42     // Try a very short timeout and lock breaking.
43     $this->assertTrue($lock->acquire('system_test_lock_acquire', 0.5), 'Lock acquired by this request.', 'Lock');
44     sleep(1);
45     // The other request should break our lock.
46     $this->drupalGet('system-test/lock-acquire');
47     $this->assertText($lock_acquired, 'Lock acquired by the other request, breaking our lock.', 'Lock');
48     // We cannot renew it, since the other thread took it.
49     $this->assertFalse($lock->acquire('system_test_lock_acquire'), 'Lock cannot be extended by this request.', 'Lock');
50
51     // Check the shut-down function.
52     $lock_acquired_exit = 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockExit()';
53     $this->drupalGet('system-test/lock-exit');
54     $this->assertText($lock_acquired_exit, 'Lock acquired by the other request before exit.', 'Lock');
55     $this->assertTrue($lock->acquire('system_test_lock_exit'), 'Lock acquired by this request after the other request exits.', 'Lock');
56   }
57
58   /**
59    * Tests that the persistent lock is persisted between requests.
60    */
61   public function testPersistentLock() {
62     $persistent_lock = $this->container->get('lock.persistent');
63     // Get a persistent lock.
64     $this->drupalGet('system-test/lock-persist/lock1');
65     $this->assertText('TRUE: Lock successfully acquired in SystemTestController::lockPersist()');
66     // Ensure that a shutdown function has not released the lock.
67     $this->assertFalse($persistent_lock->lockMayBeAvailable('lock1'));
68     $this->drupalGet('system-test/lock-persist/lock1');
69     $this->assertText('FALSE: Lock not acquired in SystemTestController::lockPersist()');
70
71     // Get another persistent lock.
72     $this->drupalGet('system-test/lock-persist/lock2');
73     $this->assertText('TRUE: Lock successfully acquired in SystemTestController::lockPersist()');
74     $this->assertFalse($persistent_lock->lockMayBeAvailable('lock2'));
75
76     // Release the first lock and try getting it again.
77     $persistent_lock->release('lock1');
78     $this->drupalGet('system-test/lock-persist/lock1');
79     $this->assertText('TRUE: Lock successfully acquired in SystemTestController::lockPersist()');
80   }
81
82 }