3 namespace Drupal\KernelTests\Core\Database;
6 * Tests the update query builder.
10 class UpdateTest extends DatabaseTestBase {
13 * Confirms that we can update a single record successfully.
15 public function testSimpleUpdate() {
16 $num_updated = db_update('test')
17 ->fields(['name' => 'Tiffany'])
20 $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
22 $saved_name = db_query('SELECT name FROM {test} WHERE id = :id', [':id' => 1])->fetchField();
23 $this->assertIdentical($saved_name, 'Tiffany', 'Updated name successfully.');
27 * Confirms updating to NULL.
29 public function testSimpleNullUpdate() {
30 $this->ensureSampleDataNull();
31 $num_updated = db_update('test_null')
32 ->fields(['age' => NULL])
33 ->condition('name', 'Kermit')
35 $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
37 $saved_age = db_query('SELECT age FROM {test_null} WHERE name = :name', [':name' => 'Kermit'])->fetchField();
38 $this->assertNull($saved_age, 'Updated name successfully.');
42 * Confirms that we can update multiple records successfully.
44 public function testMultiUpdate() {
45 $num_updated = db_update('test')
46 ->fields(['job' => 'Musician'])
47 ->condition('job', 'Singer')
49 $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
51 $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', [':job' => 'Musician'])->fetchField();
52 $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
56 * Confirms that we can update multiple records with a non-equality condition.
58 public function testMultiGTUpdate() {
59 $num_updated = db_update('test')
60 ->fields(['job' => 'Musician'])
61 ->condition('age', 26, '>')
63 $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
65 $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', [':job' => 'Musician'])->fetchField();
66 $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
70 * Confirms that we can update multiple records with a where call.
72 public function testWhereUpdate() {
73 $num_updated = db_update('test')
74 ->fields(['job' => 'Musician'])
75 ->where('age > :age', [':age' => 26])
77 $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
79 $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', [':job' => 'Musician'])->fetchField();
80 $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
84 * Confirms that we can stack condition and where calls.
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.');
94 $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', [':job' => 'Musician'])->fetchField();
95 $this->assertIdentical($num_matches, '1', 'Updated fields successfully.');
99 * Tests updating with expressions.
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')
107 $this->assertIdentical($num_rows, 4, 'Updated 4 records.');
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.');
114 * Tests return value on update.
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'])
127 $this->assertIdentical($num_rows, 5, 'Correctly returned 5 affected rows.');
131 * Confirm that we can update the primary key of a record successfully.
133 public function testPrimaryKeyUpdate() {
134 $num_updated = db_update('test')
135 ->fields(['id' => 42, 'name' => 'John'])
138 $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
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.');
145 * Confirm that we can update values in a column with special name.
147 public function testSpecialColumnUpdate() {
148 $num_updated = db_update('test_special_columns')
149 ->fields(['offset' => 'New offset value'])
152 $this->assertIdentical($num_updated, 1, 'Updated 1 special column record.');
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.');