Unverified Commit fb209270 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1476782 by daffie, droplet, donquixote, Katiemouse, Lendude, lnunesbr,...

Issue #1476782 by daffie, droplet, donquixote, Katiemouse, Lendude, lnunesbr, ekl1773, init90, jhedstrom, catch, clemens.tolboom, amateescu: DatabaseStatementPrefetch::current PHP function array_unshift() are used incorrectly

(cherry picked from commit 6add1ca7)
parent 63110a6e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -282,7 +282,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
Original line number Diff line number Diff line
@@ -54,6 +54,44 @@ function database_test_schema() {
    ],
  ];

  $schema['test_classtype'] = [
    'description' => 'A duplicate version of the test table, used for fetch_style PDO::FETCH_CLASSTYPE tests.',
    'fields' => [
      'classname' => [
        'description' => "A custom class name",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'name' => [
        'description' => "A person's name",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'age' => [
        'description' => "The person's age",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'job' => [
        'description' => "The person's job",
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
    ],
    'primary key' => ['job'],
    'indexes' => [
      'ages' => ['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'] = [
+10 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ protected function setUp() {
    $this->connection = Database::getConnection();
    $this->installSchema('database_test', [
      'test',
      'test_classtype',
      'test_people',
      'test_people_copy',
      'test_one_blob',
@@ -100,6 +101,15 @@ public static function addSampleData() {
      ])
      ->execute();

    $connection->insert('test_classtype')
      ->fields([
        'classname' => 'Drupal\Tests\system\Functional\Database\FakeRecord',
        'name' => 'Kay',
        'age' => 26,
        'job' => 'Web Developer',
      ])
      ->execute();

    $connection->insert('test_people')
      ->fields([
        'name' => 'Meredith',
+21 −0
Original line number Diff line number Diff line
@@ -80,6 +80,27 @@ public function testQueryFetchClass() {
    $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 \Drupal\Tests\system\Functional\Database\FakeRecord
   */
  public function testQueryFetchClasstype() {
    $records = [];
    $result = $this->connection->query('SELECT classname, name, job FROM {test_classtype} WHERE age = :age', [':age' => 26], ['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->assertSame('Kay', $record->name, 'Kay is found.');
        $this->assertSame('Web Developer', $record->job, 'A 26 year old Web Developer.');
      }
      $this->assertFalse(isset($record->classname), 'Classname field not found, as intended.');
    }

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

  /**
   * Confirms that we can fetch a record into an indexed array explicitly.
   */