Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / KeyValueStore / StorageTestBase.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\KeyValueStore;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Base class for testing key-value storages.
9  */
10 abstract class StorageTestBase extends KernelTestBase {
11
12   /**
13    * An array of random stdClass objects.
14    *
15    * @var array
16    */
17   protected $objects = [];
18
19   /**
20    * An array of data collection labels.
21    *
22    * @var array
23    */
24   protected $collections = [];
25
26   /**
27    * Whether we are using an expirable key/value store.
28    *
29    * @var bool
30    */
31   protected $factory = 'keyvalue';
32
33   protected function setUp() {
34     parent::setUp();
35
36     // Define two data collections,
37     $this->collections = [0 => 'zero', 1 => 'one'];
38
39     // Create several objects for testing.
40     for ($i = 0; $i <= 5; $i++) {
41       $this->objects[$i] = $this->randomObject();
42     }
43   }
44
45   /**
46    * Tests CRUD operations.
47    */
48   public function testCRUD() {
49     $stores = $this->createStorage();
50     // Verify that each store returns its own collection name.
51     $this->assertIdentical($stores[0]->getCollectionName(), $this->collections[0]);
52     $this->assertIdentical($stores[1]->getCollectionName(), $this->collections[1]);
53
54     // Verify that an item can be stored.
55     $stores[0]->set('foo', $this->objects[0]);
56     $this->assertTrue($stores[0]->has('foo'));
57     $this->assertIdenticalObject($this->objects[0], $stores[0]->get('foo'));
58     // Verify that the other collection is not affected.
59     $this->assertFalse($stores[1]->has('foo'));
60     $this->assertFalse($stores[1]->get('foo'));
61
62     // Verify that an item can be updated.
63     $stores[0]->set('foo', $this->objects[1]);
64     $this->assertIdenticalObject($this->objects[1], $stores[0]->get('foo'));
65     // Verify that the other collection is still not affected.
66     $this->assertFalse($stores[1]->get('foo'));
67
68     // Verify that a collection/name pair is unique.
69     $stores[1]->set('foo', $this->objects[2]);
70     $this->assertIdenticalObject($this->objects[1], $stores[0]->get('foo'));
71     $this->assertIdenticalObject($this->objects[2], $stores[1]->get('foo'));
72
73     // Verify that an item can be deleted.
74     $stores[0]->delete('foo');
75     $this->assertFalse($stores[0]->has('foo'));
76     $this->assertFalse($stores[0]->get('foo'));
77
78     // Verify that the other collection is not affected.
79     $this->assertTrue($stores[1]->has('foo'));
80     $this->assertIdenticalObject($this->objects[2], $stores[1]->get('foo'));
81     $stores[1]->delete('foo');
82     $this->assertFalse($stores[1]->get('foo'));
83
84     // Verify that multiple items can be stored.
85     $values = [
86       'foo' => $this->objects[3],
87       'bar' => $this->objects[4],
88     ];
89     $stores[0]->setMultiple($values);
90
91     // Verify that multiple items can be retrieved.
92     $result = $stores[0]->getMultiple(['foo', 'bar']);
93     foreach ($values as $j => $value) {
94       $this->assertIdenticalObject($value, $result[$j]);
95     }
96
97     // Verify that the other collection was not affected.
98     $this->assertFalse($stores[1]->get('foo'));
99     $this->assertFalse($stores[1]->get('bar'));
100
101     // Verify that all items in a collection can be retrieved.
102     // Ensure that an item with the same name exists in the other collection.
103     $stores[1]->set('foo', $this->objects[5]);
104     $result = $stores[0]->getAll();
105     // Not using assertSame(), since the order is not defined for getAll().
106     $this->assertEqual(count($result), count($values));
107     foreach ($result as $key => $value) {
108       $this->assertEqual($values[$key], $value);
109     }
110     // Verify that all items in the other collection are different.
111     $result = $stores[1]->getAll();
112     $this->assertEqual($result, ['foo' => $this->objects[5]]);
113
114     // Verify that multiple items can be deleted.
115     $stores[0]->deleteMultiple(array_keys($values));
116     $this->assertFalse($stores[0]->get('foo'));
117     $this->assertFalse($stores[0]->get('bar'));
118     $this->assertFalse($stores[0]->getMultiple(['foo', 'bar']));
119     // Verify that deleting no items does not cause an error.
120     $stores[0]->deleteMultiple([]);
121     // Verify that the item in the other collection still exists.
122     $this->assertIdenticalObject($this->objects[5], $stores[1]->get('foo'));
123
124   }
125
126   /**
127    * Tests expected behavior for non-existing keys.
128    */
129   public function testNonExistingKeys() {
130
131     $stores = $this->createStorage();
132
133     // Verify that a non-existing key returns NULL as value.
134     $this->assertNull($stores[0]->get('foo'));
135
136     // Verify that a non-existing key with a default returns the default.
137     $this->assertIdentical($stores[0]->get('foo', 'bar'), 'bar');
138
139     // Verify that a FALSE value can be stored.
140     $stores[0]->set('foo', FALSE);
141     $this->assertIdentical($stores[0]->get('foo'), FALSE);
142
143     // Verify that a deleted key returns NULL as value.
144     $stores[0]->delete('foo');
145     $this->assertNull($stores[0]->get('foo'));
146
147     // Verify that a non-existing key is not returned when getting multiple keys.
148     $stores[0]->set('bar', 'baz');
149     $values = $stores[0]->getMultiple(['foo', 'bar']);
150     $this->assertFalse(isset($values['foo']), "Key 'foo' not found.");
151     $this->assertIdentical($values['bar'], 'baz');
152   }
153
154   /**
155    * Tests the setIfNotExists() method.
156    */
157   public function testSetIfNotExists() {
158     $stores = $this->createStorage();
159
160     $key = $this->randomMachineName();
161     // Test that setIfNotExists() succeeds only the first time.
162     for ($i = 0; $i <= 1; $i++) {
163       // setIfNotExists() should be TRUE the first time (when $i is 0) and
164       // FALSE the second time (when $i is 1).
165       $this->assertEqual(!$i, $stores[0]->setIfNotExists($key, $this->objects[$i]));
166       $this->assertIdenticalObject($this->objects[0], $stores[0]->get($key));
167       // Verify that the other collection is not affected.
168       $this->assertFalse($stores[1]->get($key));
169     }
170
171     // Remove the item and try to set it again.
172     $stores[0]->delete($key);
173     $stores[0]->setIfNotExists($key, $this->objects[1]);
174     // This time it should succeed.
175     $this->assertIdenticalObject($this->objects[1], $stores[0]->get($key));
176     // Verify that the other collection is still not affected.
177     $this->assertFalse($stores[1]->get($key));
178   }
179
180   /**
181    * Tests the rename operation.
182    */
183   public function testRename() {
184     $stores = $this->createStorage();
185     $store = $stores[0];
186
187     $store->set('old', 'thing');
188     $this->assertIdentical($store->get('old'), 'thing');
189     $store->rename('old', 'new');
190     $this->assertIdentical($store->get('new'), 'thing');
191     $this->assertNull($store->get('old'));
192   }
193
194   /**
195    * Creates storage objects for each collection defined for this class.
196    *
197    * Storing the storage objects in a class member variable causes a fatal
198    * exception in DatabaseStorageExpirableTest, because in that situation
199    * garbage collection is not triggered until the test class itself is
200    * destructed, after tearDown() has deleted the database tables. Instead,
201    * create the storage objects locally in each test using this method.
202    *
203    * @see \Drupal\system\Tests\KeyValueStore\DatabaseStorageExpirable
204    * @see \Drupal\Core\KeyValueStore\DatabaseStorageExpirable::garbageCollection()
205    */
206   protected function createStorage() {
207     $stores = [];
208     foreach ($this->collections as $i => $collection) {
209       $stores[$i] = $this->container->get($this->factory)->get($collection);
210     }
211
212     return $stores;
213   }
214
215 }