Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Database / UpdateTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Database;
4
5 /**
6  * Tests the update query builder.
7  *
8  * @group Database
9  */
10 class UpdateTest extends DatabaseTestBase {
11
12   /**
13    * Confirms that we can update a single record successfully.
14    */
15   public function testSimpleUpdate() {
16     $num_updated = db_update('test')
17       ->fields(['name' => 'Tiffany'])
18       ->condition('id', 1)
19       ->execute();
20     $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
21
22     $saved_name = db_query('SELECT name FROM {test} WHERE id = :id', [':id' => 1])->fetchField();
23     $this->assertIdentical($saved_name, 'Tiffany', 'Updated name successfully.');
24   }
25
26   /**
27    * Confirms updating to NULL.
28    */
29   public function testSimpleNullUpdate() {
30     $this->ensureSampleDataNull();
31     $num_updated = db_update('test_null')
32       ->fields(['age' => NULL])
33       ->condition('name', 'Kermit')
34       ->execute();
35     $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
36
37     $saved_age = db_query('SELECT age FROM {test_null} WHERE name = :name', [':name' => 'Kermit'])->fetchField();
38     $this->assertNull($saved_age, 'Updated name successfully.');
39   }
40
41   /**
42    * Confirms that we can update multiple records successfully.
43    */
44   public function testMultiUpdate() {
45     $num_updated = db_update('test')
46       ->fields(['job' => 'Musician'])
47       ->condition('job', 'Singer')
48       ->execute();
49     $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
50
51     $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', [':job' => 'Musician'])->fetchField();
52     $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
53   }
54
55   /**
56    * Confirms that we can update multiple records with a non-equality condition.
57    */
58   public function testMultiGTUpdate() {
59     $num_updated = db_update('test')
60       ->fields(['job' => 'Musician'])
61       ->condition('age', 26, '>')
62       ->execute();
63     $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
64
65     $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', [':job' => 'Musician'])->fetchField();
66     $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
67   }
68
69   /**
70    * Confirms that we can update multiple records with a where call.
71    */
72   public function testWhereUpdate() {
73     $num_updated = db_update('test')
74       ->fields(['job' => 'Musician'])
75       ->where('age > :age', [':age' => 26])
76       ->execute();
77     $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
78
79     $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', [':job' => 'Musician'])->fetchField();
80     $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
81   }
82
83   /**
84    * Confirms that we can stack condition and where calls.
85    */
86   public function testWhereAndConditionUpdate() {
87     $update = db_update('test')
88       ->fields(['job' => 'Musician'])
89       ->where('age > :age', [':age' => 26])
90       ->condition('name', 'Ringo');
91     $num_updated = $update->execute();
92     $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
93
94     $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', [':job' => 'Musician'])->fetchField();
95     $this->assertIdentical($num_matches, '1', 'Updated fields successfully.');
96   }
97
98   /**
99    * Tests updating with expressions.
100    */
101   public function testExpressionUpdate() {
102     // Ensure that expressions are handled properly. This should set every
103     // record's age to a square of itself.
104     $num_rows = db_update('test')
105       ->expression('age', 'age * age')
106       ->execute();
107     $this->assertIdentical($num_rows, 4, 'Updated 4 records.');
108
109     $saved_name = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => pow(26, 2)])->fetchField();
110     $this->assertIdentical($saved_name, 'Paul', 'Successfully updated values using an algebraic expression.');
111   }
112
113   /**
114    * Tests return value on update.
115    */
116   public function testUpdateAffectedRows() {
117     // At 5am in the morning, all band members but those with a priority 1 task
118     // are sleeping. So we set their tasks to 'sleep'. 5 records match the
119     // condition and therefore are affected by the query, even though two of
120     // them actually don't have to be changed because their value was already
121     // 'sleep'. Still, execute() should return 5 affected rows, not only 3,
122     // because that's cross-db expected behavior.
123     $num_rows = db_update('test_task')
124       ->condition('priority', 1, '<>')
125       ->fields(['task' => 'sleep'])
126       ->execute();
127     $this->assertIdentical($num_rows, 5, 'Correctly returned 5 affected rows.');
128   }
129
130   /**
131    * Confirm that we can update the primary key of a record successfully.
132    */
133   public function testPrimaryKeyUpdate() {
134     $num_updated = db_update('test')
135       ->fields(['id' => 42, 'name' => 'John'])
136       ->condition('id', 1)
137       ->execute();
138     $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
139
140     $saved_name = db_query('SELECT name FROM {test} WHERE id = :id', [':id' => 42])->fetchField();
141     $this->assertIdentical($saved_name, 'John', 'Updated primary key successfully.');
142   }
143
144   /**
145    * Confirm that we can update values in a column with special name.
146    */
147   public function testSpecialColumnUpdate() {
148     $num_updated = db_update('test_special_columns')
149       ->fields(['offset' => 'New offset value'])
150       ->condition('id', 1)
151       ->execute();
152     $this->assertIdentical($num_updated, 1, 'Updated 1 special column record.');
153
154     $saved_value = db_query('SELECT "offset" FROM {test_special_columns} WHERE id = :id', [':id' => 1])->fetchField();
155     $this->assertIdentical($saved_value, 'New offset value', 'Updated special column name value successfully.');
156   }
157
158 }