DeleteTruncateTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Drupal\KernelTests\Core\Database;
  3. /**
  4. * Tests delete and truncate queries.
  5. *
  6. * The DELETE tests are not as extensive, as all of the interesting code for
  7. * DELETE queries is in the conditional which is identical to the UPDATE and
  8. * SELECT conditional handling.
  9. *
  10. * The TRUNCATE tests are not extensive either, because the behavior of
  11. * TRUNCATE queries is not consistent across database engines. We only test
  12. * that a TRUNCATE query actually deletes all rows from the target table.
  13. *
  14. * @group Database
  15. */
  16. class DeleteTruncateTest extends DatabaseTestBase {
  17. /**
  18. * Confirms that we can use a subselect in a delete successfully.
  19. */
  20. public function testSubselectDelete() {
  21. $num_records_before = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
  22. $pid_to_delete = db_query("SELECT * FROM {test_task} WHERE task = 'sleep'")->fetchField();
  23. $subquery = db_select('test', 't')
  24. ->fields('t', ['id'])
  25. ->condition('t.id', [$pid_to_delete], 'IN');
  26. $delete = db_delete('test_task')
  27. ->condition('task', 'sleep')
  28. ->condition('pid', $subquery, 'IN');
  29. $num_deleted = $delete->execute();
  30. $this->assertEqual($num_deleted, 1, 'Deleted 1 record.');
  31. $num_records_after = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
  32. $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
  33. }
  34. /**
  35. * Confirms that we can delete a single record successfully.
  36. */
  37. public function testSimpleDelete() {
  38. $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  39. $num_deleted = db_delete('test')
  40. ->condition('id', 1)
  41. ->execute();
  42. $this->assertIdentical($num_deleted, 1, 'Deleted 1 record.');
  43. $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  44. $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
  45. }
  46. /**
  47. * Confirms that we can truncate a whole table successfully.
  48. */
  49. public function testTruncate() {
  50. $num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
  51. $this->assertTrue($num_records_before > 0, 'The table is not empty.');
  52. db_truncate('test')->execute();
  53. $num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
  54. $this->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
  55. }
  56. /**
  57. * Confirms that we can delete a single special column name record successfully.
  58. */
  59. public function testSpecialColumnDelete() {
  60. $num_records_before = db_query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
  61. $num_deleted = db_delete('test_special_columns')
  62. ->condition('id', 1)
  63. ->execute();
  64. $this->assertIdentical($num_deleted, 1, 'Deleted 1 special column record.');
  65. $num_records_after = db_query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
  66. $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
  67. }
  68. }