UpdateTest.php 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Database\UpdateTest.
 */

namespace Drupal\system\Tests\Database;

/**
11
 * Tests the update query builder.
12 13 14 15 16 17 18 19 20 21 22 23
 */
class UpdateTest extends DatabaseTestBase {

  public static function getInfo() {
    return array(
      'name' => 'Update tests',
      'description' => 'Test the Update query builder.',
      'group' => 'Database',
    );
  }

  /**
24
   * Confirms that we can update a single record successfully.
25 26 27 28 29 30
   */
  function testSimpleUpdate() {
    $num_updated = db_update('test')
      ->fields(array('name' => 'Tiffany'))
      ->condition('id', 1)
      ->execute();
31
    $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
32 33

    $saved_name = db_query('SELECT name FROM {test} WHERE id = :id', array(':id' => 1))->fetchField();
34
    $this->assertIdentical($saved_name, 'Tiffany', 'Updated name successfully.');
35 36 37
  }

  /**
38
   * Confirms updating to NULL.
39 40 41 42 43 44 45
   */
  function testSimpleNullUpdate() {
    $this->ensureSampleDataNull();
    $num_updated = db_update('test_null')
      ->fields(array('age' => NULL))
      ->condition('name', 'Kermit')
      ->execute();
46
    $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
47 48

    $saved_age = db_query('SELECT age FROM {test_null} WHERE name = :name', array(':name' => 'Kermit'))->fetchField();
49
    $this->assertNull($saved_age, 'Updated name successfully.');
50 51 52
  }

  /**
53
   * Confirms that we can update multiple records successfully.
54 55 56 57 58 59
   */
  function testMultiUpdate() {
    $num_updated = db_update('test')
      ->fields(array('job' => 'Musician'))
      ->condition('job', 'Singer')
      ->execute();
60
    $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
61 62

    $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
63
    $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
64 65 66
  }

  /**
67
   * Confirms that we can update multiple records with a non-equality condition.
68 69 70 71 72 73
   */
  function testMultiGTUpdate() {
    $num_updated = db_update('test')
      ->fields(array('job' => 'Musician'))
      ->condition('age', 26, '>')
      ->execute();
74
    $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
75 76

    $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
77
    $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
78 79 80
  }

  /**
81
   * Confirms that we can update multiple records with a where call.
82 83 84 85 86 87
   */
  function testWhereUpdate() {
    $num_updated = db_update('test')
      ->fields(array('job' => 'Musician'))
      ->where('age > :age', array(':age' => 26))
      ->execute();
88
    $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
89 90

    $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
91
    $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
92 93 94
  }

  /**
95
   * Confirms that we can stack condition and where calls.
96 97 98 99 100 101 102
   */
  function testWhereAndConditionUpdate() {
    $update = db_update('test')
      ->fields(array('job' => 'Musician'))
      ->where('age > :age', array(':age' => 26))
      ->condition('name', 'Ringo');
    $num_updated = $update->execute();
103
    $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
104 105

    $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
106
    $this->assertIdentical($num_matches, '1', 'Updated fields successfully.');
107 108 109
  }

  /**
110
   * Tests updating with expressions.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
   */
  function testExpressionUpdate() {
    // Set age = 1 for a single row for this test to work.
    db_update('test')
      ->condition('id', 1)
      ->fields(array('age' => 1))
      ->execute();

    // Ensure that expressions are handled properly.  This should set every
    // record's age to a square of itself, which will change only three of the
    // four records in the table since 1*1 = 1. That means only three records
    // are modified, so we should get back 3, not 4, from execute().
    $num_rows = db_update('test')
      ->expression('age', 'age * age')
      ->execute();
126
    $this->assertIdentical($num_rows, 3, 'Number of affected rows are returned.');
127 128
  }
}