Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Database / UpsertTest.php
index 5c62f87f6bad05521f851911045ffba9ef118899..461117797f7e1123101850eb4d6118e488c55277 100644 (file)
@@ -53,4 +53,40 @@ class UpsertTest extends DatabaseTestBase {
     $this->assertEqual($person->name, 'Meredith', 'Name was not changed.');
   }
 
+  /**
+   * Tests that we can upsert records with a special named column.
+   */
+  public function testSpecialColumnUpsert() {
+    $num_records_before = $this->connection->query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
+    $upsert = $this->connection->upsert('test_special_columns')
+      ->key('id')
+      ->fields(['id', 'offset', 'function']);
+
+    // Add a new row.
+    $upsert->values([
+      'id' => 2,
+      'offset' => 'Offset 2',
+      'function' => 'Function 2',
+    ]);
+
+    // Update an existing row.
+    $upsert->values([
+      'id' => 1,
+      'offset' => 'Offset 1 updated',
+      'function' => 'Function 1 updated',
+    ]);
+
+    $upsert->execute();
+    $num_records_after = $this->connection->query('SELECT COUNT(*) FROM {test_special_columns}')->fetchField();
+    $this->assertEquals($num_records_before + 1, $num_records_after, 'Rows were inserted and updated properly.');
+
+    $record = $this->connection->query('SELECT * FROM {test_special_columns} WHERE id = :id', [':id' => 1])->fetch();
+    $this->assertEquals($record->offset, 'Offset 1 updated');
+    $this->assertEquals($record->function, 'Function 1 updated');
+
+    $record = $this->connection->query('SELECT * FROM {test_special_columns} WHERE id = :id', [':id' => 2])->fetch();
+    $this->assertEquals($record->offset, 'Offset 2');
+    $this->assertEquals($record->function, 'Function 2');
+  }
+
 }