ad1d050364ac9126362b04dd80cb6128dc01fc17
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Database / DeleteTruncateTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Database;
4
5 /**
6  * Tests delete and truncate queries.
7  *
8  * The DELETE tests are not as extensive, as all of the interesting code for
9  * DELETE queries is in the conditional which is identical to the UPDATE and
10  * SELECT conditional handling.
11  *
12  * The TRUNCATE tests are not extensive either, because the behavior of
13  * TRUNCATE queries is not consistent across database engines. We only test
14  * that a TRUNCATE query actually deletes all rows from the target table.
15  *
16  * @group Database
17  */
18 class DeleteTruncateTest extends DatabaseTestBase {
19
20   /**
21    * Confirms that we can use a subselect in a delete successfully.
22    */
23   public function testSubselectDelete() {
24     $num_records_before = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
25     $pid_to_delete = db_query("SELECT * FROM {test_task} WHERE task = 'sleep'")->fetchField();
26
27     $subquery = db_select('test', 't')
28       ->fields('t', ['id'])
29       ->condition('t.id', [$pid_to_delete], 'IN');
30     $delete = db_delete('test_task')
31       ->condition('task', 'sleep')
32       ->condition('pid', $subquery, 'IN');
33
34     $num_deleted = $delete->execute();
35     $this->assertEqual($num_deleted, 1, 'Deleted 1 record.');
36
37     $num_records_after = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
38     $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
39   }
40
41   /**
42    * Confirms that we can delete a single record successfully.
43    */
44   public function testSimpleDelete() {
45     $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
46
47     $num_deleted = db_delete('test')
48       ->condition('id', 1)
49       ->execute();
50     $this->assertIdentical($num_deleted, 1, 'Deleted 1 record.');
51
52     $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
53     $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
54   }
55
56   /**
57    * Confirms that we can truncate a whole table successfully.
58    */
59   public function testTruncate() {
60     $num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
61     $this->assertTrue($num_records_before > 0, 'The table is not empty.');
62
63     db_truncate('test')->execute();
64
65     $num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
66     $this->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
67   }
68
69   /**
70    * Confirms that we can truncate a whole table while in transaction.
71    */
72   public function testTruncateInTransaction() {
73     // This test won't work right if transactions are not supported.
74     if (!$this->connection->supportsTransactions()) {
75       $this->markTestSkipped('The database driver does not support transactions.');
76     }
77
78     $num_records_before = $this->connection->select('test')->countQuery()->execute()->fetchField();
79     $this->assertGreaterThan(0, $num_records_before, 'The table is not empty.');
80
81     $transaction = $this->connection->startTransaction('test_truncate_in_transaction');
82     $this->connection->insert('test')
83       ->fields([
84         'name' => 'Freddie',
85         'age' => 45,
86         'job' => 'Great singer',
87       ])
88       ->execute();
89     $num_records_after_insert = $this->connection->select('test')->countQuery()->execute()->fetchField();
90     $this->assertEquals($num_records_before + 1, $num_records_after_insert);
91
92     $this->connection->truncate('test')->execute();
93
94     // Checks that there are no records left in the table, and transaction is
95     // still active.
96     $this->assertTrue($this->connection->inTransaction());
97     $num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
98     $this->assertEquals(0, $num_records_after);
99
100     // Close the transaction, and check that there are still no records in the
101     // table.
102     $transaction = NULL;
103     $this->assertFalse($this->connection->inTransaction());
104     $num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
105     $this->assertEquals(0, $num_records_after);
106   }
107
108   /**
109    * Confirms that transaction rollback voids a truncate operation.
110    */
111   public function testTruncateTransactionRollback() {
112     // This test won't work right if transactions are not supported.
113     if (!$this->connection->supportsTransactions()) {
114       $this->markTestSkipped('The database driver does not support transactions.');
115     }
116
117     $num_records_before = $this->connection->select('test')->countQuery()->execute()->fetchField();
118     $this->assertGreaterThan(0, $num_records_before, 'The table is not empty.');
119
120     $transaction = $this->connection->startTransaction('test_truncate_in_transaction');
121     $this->connection->insert('test')
122       ->fields([
123         'name' => 'Freddie',
124         'age' => 45,
125         'job' => 'Great singer',
126       ])
127       ->execute();
128     $num_records_after_insert = $this->connection->select('test')->countQuery()->execute()->fetchField();
129     $this->assertEquals($num_records_before + 1, $num_records_after_insert);
130
131     $this->connection->truncate('test')->execute();
132
133     // Checks that there are no records left in the table, and transaction is
134     // still active.
135     $this->assertTrue($this->connection->inTransaction());
136     $num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
137     $this->assertEquals(0, $num_records_after);
138
139     // Roll back the transaction, and check that we are back to status before
140     // insert and truncate.
141     $this->connection->rollBack();
142     $this->assertFalse($this->connection->inTransaction());
143     $num_records_after = $this->connection->select('test')->countQuery()->execute()->fetchField();
144     $this->assertEquals($num_records_before, $num_records_after);
145   }
146
147   /**
148    * Confirms that we can delete a single special column name record successfully.
149    */
150   public function testSpecialColumnDelete() {
151     $num_records_before = db_query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
152
153     $num_deleted = db_delete('test_special_columns')
154       ->condition('id', 1)
155       ->execute();
156     $this->assertIdentical($num_deleted, 1, 'Deleted 1 special column record.');
157
158     $num_records_after = db_query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
159     $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
160   }
161
162 }