Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Lock / LockTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Lock;
4
5 use Drupal\Core\Lock\DatabaseLockBackend;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests the Database lock backend.
10  *
11  * @group Lock
12  */
13 class LockTest extends KernelTestBase {
14
15   /**
16    * Database lock backend to test.
17    *
18    * @var \Drupal\Core\Lock\DatabaseLockBackend
19    */
20   protected $lock;
21
22   protected function setUp() {
23     parent::setUp();
24     $this->lock = new DatabaseLockBackend($this->container->get('database'));
25   }
26
27   /**
28    * Tests backend release functionality.
29    */
30   public function testBackendLockRelease() {
31     $success = $this->lock->acquire('lock_a');
32     $this->assertTrue($success, 'Could acquire first lock.');
33
34     // This function is not part of the backend, but the default database
35     // backend implement it, we can here use it safely.
36     $is_free = $this->lock->lockMayBeAvailable('lock_a');
37     $this->assertFalse($is_free, 'First lock is unavailable.');
38
39     $this->lock->release('lock_a');
40     $is_free = $this->lock->lockMayBeAvailable('lock_a');
41     $this->assertTrue($is_free, 'First lock has been released.');
42
43     $success = $this->lock->acquire('lock_b');
44     $this->assertTrue($success, 'Could acquire second lock.');
45
46     $success = $this->lock->acquire('lock_b');
47     $this->assertTrue($success, 'Could acquire second lock a second time within the same request.');
48
49     $this->lock->release('lock_b');
50
51     // Test acquiring an releasing a lock with a long key (over 255 chars).
52     $long_key = 'long_key:BZoMiSf9IIPULsJ98po18TxJ6T4usd3MZrLE0d3qMgG6iAgDlOi1G3oMap7zI5df84l7LtJBg4bOj6XvpO6vDRmP5h5QbA0Bj9rVFiPIPAIQZ9qFvJqTALiK1OR3GpOkWQ4vgEA4LkY0UfznrWBeuK7IWZfv1um6DLosnVXd1z1cJjvbEUqYGJj92rwHfhYihLm8IO9t3P2gAvEkH5Mhc8GBoiTsIDnP01Te1kxGFHO3RuvJIxPnHmZtSdBggmuVN7x9';
53
54     $success = $this->lock->acquire($long_key);
55     $this->assertTrue($success, 'Could acquire long key lock.');
56
57     // This function is not part of the backend, but the default database
58     // backend implement it, we can here use it safely.
59     $is_free = $this->lock->lockMayBeAvailable($long_key);
60     $this->assertFalse($is_free, 'Long key lock is unavailable.');
61
62     $this->lock->release($long_key);
63     $is_free = $this->lock->lockMayBeAvailable($long_key);
64     $this->assertTrue($is_free, 'Long key lock has been released.');
65   }
66
67   /**
68    * Tests backend release functionality.
69    */
70   public function testBackendLockReleaseAll() {
71     $success = $this->lock->acquire('lock_a');
72     $this->assertTrue($success, 'Could acquire first lock.');
73
74     $success = $this->lock->acquire('lock_b');
75     $this->assertTrue($success, 'Could acquire second lock.');
76
77     $this->lock->releaseAll();
78
79     $is_free = $this->lock->lockMayBeAvailable('lock_a');
80     $this->assertTrue($is_free, 'First lock has been released.');
81
82     $is_free = $this->lock->lockMayBeAvailable('lock_b');
83     $this->assertTrue($is_free, 'Second lock has been released.');
84   }
85
86 }