IgnoreReplicaSubscriberTest.php 1.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php

/**
 * Contains \Drupal\system\Tests\System\IgnoreReplicaSubscriberTest.
 */

namespace Drupal\system\Tests\System;

use Drupal\Core\Database\Database;
use Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber;
use Drupal\Core\DrupalKernel;
use Drupal\simpletest\UnitTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
 * Tests the event subscriber that disables the replica database.
 */
20
class IgnoreReplicaSubscriberTest extends UnitTestBase {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  public static function getInfo() {
    return array(
      'name' => 'Replica database ignoring event listener',
      'description' => 'Tests that ReplicaDatabaseIgnoreSubscriber functions correctly.',
      'group' => 'System',
    );
  }

  /**
   * Tests \Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber::checkReplicaServer().
   */
  function testSystemInitIgnoresSecondaries() {
    // Clone the master credentials to a replica connection.
    // Note this will result in two independent connection objects that happen
    // to point to the same place.
    $connection_info = Database::getConnectionInfo('default');
    Database::addConnectionInfo('default', 'replica', $connection_info['default']);

    db_ignore_replica();
    $kernel = new DrupalKernel('testing', drupal_classloader(), FALSE);
    $event = new GetResponseEvent($kernel, Request::create('http://example.com'), HttpKernelInterface::MASTER_REQUEST);
    $subscriber = new ReplicaDatabaseIgnoreSubscriber();
    $subscriber->checkReplicaServer($event);

    $db1 = Database::getConnection('default', 'default');
    $db2 = Database::getConnection('replica', 'default');

    $this->assertIdentical($db1, $db2, 'System Init ignores secondaries when requested.');
  }
}