Verified Commit 782debf1 authored by Juraj Nemec's avatar Juraj Nemec
Browse files

Issue #3064734 by sjerdo, geoffreyr, karan_mudi:...

Issue #3064734 by sjerdo, geoffreyr, karan_mudi: DatabaseStatementPrefetch::current PHP function array_unshift() are used incorrectly
parent 32020ecb
Loading
Loading
Loading
Loading
+1 −1
Changes for includes/database/prefetch.inc: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -286,7 +286,7 @@ public function current() {
        case PDO::FETCH_OBJ:
          return (object) $this->currentRow;
        case PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE:
          $class_name = array_unshift($this->currentRow);
          $class_name = array_shift($this->currentRow);
          // Deliberate no break.
        case PDO::FETCH_CLASS:
          if (!isset($class_name)) {
+38 −0
Changes for modules/simpletest/tests/database_test.install: 38 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -54,6 +54,44 @@ function database_test_schema() {
    ),
  );

  $schema['test_classtype'] = array(
    'description' => 'A duplicate version of the test table, used for fetch_style PDO::FETCH_CLASSTYPE tests.',
    'fields' => array(
      'classname' => array(
        'description' => "A custom class name",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'name' => array(
        'description' => "A person's name",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'age' => array(
        'description' => "The person's age",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'job' => array(
        'description' => "The person's job",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('job'),
    'indexes' => array(
      'ages' => array('age'),
    ),
  );

  // This is an alternate version of the same table that is structured the same
  // but has a non-serial Primary Key.
  $schema['test_people'] = array(
+31 −0
Changes for modules/simpletest/tests/database_test.test: 31 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ class DatabaseTestCase extends DrupalWebTestCase {
    parent::setUp('database_test');

    $schema['test'] = drupal_get_schema('test');
    $schema['test_classtype'] = drupal_get_schema('test_classtype');
    $schema['test_people'] = drupal_get_schema('test_people');
    $schema['test_people_copy'] = drupal_get_schema('test_people_copy');
    $schema['test_one_blob'] = drupal_get_schema('test_one_blob');
@@ -118,6 +119,15 @@ class DatabaseTestCase extends DrupalWebTestCase {
      ))
      ->execute();

    db_insert('test_classtype')
      ->fields(array(
        'classname' => 'FakeRecord',
        'name' => 'Kay',
        'age' => 26,
        'job' => 'Web Developer',
      ))
      ->execute();

    db_insert('test_people')
      ->fields(array(
        'name' => 'Meredith',
@@ -407,6 +417,27 @@ class DatabaseFetchTestCase extends DatabaseTestCase {

    $this->assertIdentical(count($records), 1, 'There is only one record.');
  }

  /**
   * Confirms that we can fetch a record into a new instance of a custom class.
   * The name of the class is determined from a value of the first column.
   *
   * @see FakeRecord
   */
  function testQueryFetchClasstype() {
    $records = array();
    $result = db_query('SELECT classname, name, job FROM {test_classtype} WHERE age = :age', array(':age' => 26), array('fetch' => PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE));
    foreach ($result as $record) {
      $records[] = $record;
      if ($this->assertTrue($record instanceof FakeRecord, 'Record is an object of class FakeRecord.')) {
        $this->assertIdentical($record->name, 'Kay', 'Kay is found.');
        $this->assertIdentical($record->job, 'Web Developer', 'A 26 year old Web Developer.');
      }
      $this->assertFalse(isset($record->classname), 'Classname field not found, as intended.');
    }

    $this->assertIdentical(count($records), 1, 'There is only one record.');
  }
}

/**