UpdateTest.php 5.6 KB

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