class BackendChainImplementationUnitTest

Tests implementation-specific functionality of the BackendChain backend.

Hierarchy

Expanded class hierarchy of BackendChainImplementationUnitTest

File

drupal/core/modules/system/lib/Drupal/system/Tests/Cache/BackendChainImplementationUnitTest.php, line 18
Definition of Drupal\system\Tests\Cache\BackendChainImplementationUnitTest.

Namespace

Drupal\system\Tests\Cache
View source
class BackendChainImplementationUnitTest extends UnitTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Backend chain implementation',
      'description' => 'Unit test of backend chain implementation specifics.',
      'group' => 'Cache',
    );
  }

  /**
   * Chain that will be heavily tested.
   *
   * @var Drupal\Core\Cache\BackendChain
   */
  protected $chain;

  /**
   * First backend in the chain.
   *
   * @var Drupal\Core\Cache\CacheBackendInterface
   */
  protected $firstBackend;

  /**
   * Second backend in the chain.
   *
   * @var Drupal\Core\Cache\CacheBackendInterface
   */
  protected $secondBackend;

  /**
   * Third backend in the chain.
   *
   * @var Drupal\Core\Cache\CacheBackendInterface
   */
  protected $thirdBackend;
  public function setUp() {
    parent::setUp();

    // Set up three memory backends to be used in the chain.
    $this->firstBackend = new MemoryBackend('foo');
    $this->secondBackend = new MemoryBackend('bar');
    $this->thirdBackend = new MemoryBackend('baz');

    // Set an initial fixed dataset for all testing. The next three data
    // collections will test two edge cases (last backend has the data, and
    // first backend has the data) and will test a normal use case (middle
    // backend has the data). We should have a complete unit test with those.
    // Note that in all cases, when the same key is set on more than one
    // backend, the values are voluntarily different, this ensures in which
    // backend we actually fetched the key when doing get calls.
    // Set a key present on all backends (for delete).
    $this->firstBackend
      ->set('t123', 1231);
    $this->secondBackend
      ->set('t123', 1232);
    $this->thirdBackend
      ->set('t123', 1233);

    // Set a key present on the second and the third (for get), those two will
    // be different, this will ensure from where we get the key.
    $this->secondBackend
      ->set('t23', 232);
    $this->thirdBackend
      ->set('t23', 233);

    // Set a key on only the third, we will ensure propagation using this one.
    $this->thirdBackend
      ->set('t3', 33);

    // Create the chain.
    $this->chain = new BackendChain('foobarbaz');
    $this->chain
      ->appendBackend($this->firstBackend)
      ->appendBackend($this->secondBackend)
      ->appendBackend($this->thirdBackend);
  }

  /**
   * Test the get feature.
   */
  public function testGet() {
    $cached = $this->chain
      ->get('t123');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Got key that is on all backends');
    $this
      ->assertIdentical(1231, $cached->data, 'Got the key from the backend 1');
    $cached = $this->chain
      ->get('t23');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Got key that is on backends 2 and 3');
    $this
      ->assertIdentical(232, $cached->data, 'Got the key from the backend 2');
    $cached = $this->chain
      ->get('t3');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Got key that is on the backend 3');
    $this
      ->assertIdentical(33, $cached->data, 'Got the key from the backend 3');
  }

  /**
   * Test the get multiple feature.
   */
  public function testGetMultiple() {
    $cids = array(
      't123',
      't23',
      't3',
      't4',
    );
    $ret = $this->chain
      ->getMultiple($cids);
    $this
      ->assertIdentical($ret['t123']->data, 1231, 'Got key 123 and value is from the first backend');
    $this
      ->assertIdentical($ret['t23']->data, 232, 'Got key 23 and value is from the second backend');
    $this
      ->assertIdentical($ret['t3']->data, 33, 'Got key 3 and value is from the third backend');
    $this
      ->assertFalse(array_key_exists('t4', $ret), "Didn't get the nonexistent key");
    $this
      ->assertFalse(in_array('t123', $cids), "Existing key 123 has been removed from &\$cids");
    $this
      ->assertFalse(in_array('t23', $cids), "Existing key 23 has been removed from &\$cids");
    $this
      ->assertFalse(in_array('t3', $cids), "Existing key 3 has been removed from &\$cids");
    $this
      ->assert(in_array('t4', $cids), "Non existing key 4 is still in &\$cids");
  }

  /**
   * Test that set will propagate.
   */
  public function testSet() {
    $this->chain
      ->set('test', 123);
    $cached = $this->firstBackend
      ->get('test');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test key is in the first backend');
    $this
      ->assertIdentical(123, $cached->data, 'Test key has the right value');
    $cached = $this->secondBackend
      ->get('test');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test key is in the second backend');
    $this
      ->assertIdentical(123, $cached->data, 'Test key has the right value');
    $cached = $this->thirdBackend
      ->get('test');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test key is in the third backend');
    $this
      ->assertIdentical(123, $cached->data, 'Test key has the right value');
  }

  /**
   * Test that delete will propagate.
   */
  public function testDelete() {
    $this->chain
      ->set('test', 5);
    $cached = $this->firstBackend
      ->get('test');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test key has been added to the first backend');
    $cached = $this->secondBackend
      ->get('test');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test key has been added to the first backend');
    $cached = $this->thirdBackend
      ->get('test');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test key has been added to the first backend');
    $this->chain
      ->delete('test');
    $cached = $this->firstBackend
      ->get('test');
    $this
      ->assertIdentical(FALSE, $cached, 'Test key is removed from the first backend');
    $cached = $this->secondBackend
      ->get('test');
    $this
      ->assertIdentical(FALSE, $cached, 'Test key is removed from the second backend');
    $cached = $this->thirdBackend
      ->get('test');
    $this
      ->assertIdentical(FALSE, $cached, 'Test key is removed from the third backend');
  }

  /**
   * Ensure get values propagation to previous backends.
   */
  public function testGetHasPropagated() {
    $this->chain
      ->get('t23');
    $cached = $this->firstBackend
      ->get('t23');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test 2 has been propagated to the first backend');
    $this->chain
      ->get('t3');
    $cached = $this->firstBackend
      ->get('t3');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test 3 has been propagated to the first backend');
    $cached = $this->secondBackend
      ->get('t3');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test 3 has been propagated to the second backend');
  }

  /**
   * Ensure get multiple values propagation to previous backends.
   */
  public function testGetMultipleHasPropagated() {
    $cids = array(
      't3',
      't23',
    );
    $this->chain
      ->getMultiple($cids);
    $cached = $this->firstBackend
      ->get('t3');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test 3 has been propagated to the first backend');
    $this
      ->assertIdentical(33, $cached->data, 'And value has been kept');
    $cached = $this->secondBackend
      ->get('t3');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test 3 has been propagated to the second backend');
    $this
      ->assertIdentical(33, $cached->data, 'And value has been kept');
    $cached = $this->firstBackend
      ->get('t23');
    $this
      ->assertNotIdentical(FALSE, $cached, 'Test 2 has been propagated to the first backend');
    $this
      ->assertIdentical(232, $cached->data, 'And value has been kept');
  }

  /**
   * Test that the chain is not empty when at least one backend has data.
   */
  public function testNotEmptyIfOneBackendHasTheKey() {
    $this
      ->assertFalse($this->chain
      ->isEmpty(), 'Chain is not empty');

    // This is the only test that needs to start with an empty chain.
    $this->chain
      ->deleteAll();
    $this
      ->assert($this->chain
      ->isEmpty(), 'Chain have been emptied by the deleteAll() call');
    $this->secondBackend
      ->set('test', 5);
    $this
      ->assertFalse($this->chain
      ->isEmpty(), 'Chain is not empty anymore now that the second backend has something');
  }

  /**
   * Test that the delete all operation is propagated to all backends in the chain.
   */
  public function testDeleteAllPropagation() {

    // Set both expiring and permanent keys.
    $this->chain
      ->set('test1', 1, CacheBackendInterface::CACHE_PERMANENT);
    $this->chain
      ->set('test2', 3, time() + 1000);
    $this->chain
      ->deleteAll();
    $this
      ->assertTrue($this->firstBackend
      ->isEmpty(), 'First backend is empty after delete all.');
    $this
      ->assertTrue($this->secondBackend
      ->isEmpty(), 'Second backend is empty after delete all.');
    $this
      ->assertTrue($this->thirdBackend
      ->isEmpty(), 'Third backend is empty after delete all.');
  }

  /**
   * Test that the delete tags operation is propagated to all backends
   * in the chain.
   */
  public function testDeleteTagsPropagation() {

    // Create two cache entries with the same tag and tag value.
    $this->chain
      ->set('test_cid_clear1', 'foo', CacheBackendInterface::CACHE_PERMANENT, array(
      'test_tag' => 2,
    ));
    $this->chain
      ->set('test_cid_clear2', 'foo', CacheBackendInterface::CACHE_PERMANENT, array(
      'test_tag' => 2,
    ));
    $this
      ->assertNotIdentical(FALSE, $this->firstBackend
      ->get('test_cid_clear1') && $this->firstBackend
      ->get('test_cid_clear2') && $this->secondBackend
      ->get('test_cid_clear1') && $this->secondBackend
      ->get('test_cid_clear2') && $this->thirdBackend
      ->get('test_cid_clear1') && $this->thirdBackend
      ->get('test_cid_clear2'), 'Two cache items were created in all backends.');

    // Invalidate test_tag of value 1. This should invalidate both entries.
    $this->chain
      ->deleteTags(array(
      'test_tag' => 2,
    ));
    $this
      ->assertIdentical(FALSE, $this->firstBackend
      ->get('test_cid_clear1') && $this->firstBackend
      ->get('test_cid_clear2') && $this->secondBackend
      ->get('test_cid_clear1') && $this->secondBackend
      ->get('test_cid_clear2') && $this->thirdBackend
      ->get('test_cid_clear1') && $this->thirdBackend
      ->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.');

    // Create two cache entries with the same tag and an array tag value.
    $this->chain
      ->set('test_cid_clear1', 'foo', CacheBackendInterface::CACHE_PERMANENT, array(
      'test_tag' => array(
        1,
      ),
    ));
    $this->chain
      ->set('test_cid_clear2', 'foo', CacheBackendInterface::CACHE_PERMANENT, array(
      'test_tag' => array(
        1,
      ),
    ));
    $this
      ->assertNotIdentical(FALSE, $this->firstBackend
      ->get('test_cid_clear1') && $this->firstBackend
      ->get('test_cid_clear2') && $this->secondBackend
      ->get('test_cid_clear1') && $this->secondBackend
      ->get('test_cid_clear2') && $this->thirdBackend
      ->get('test_cid_clear1') && $this->thirdBackend
      ->get('test_cid_clear2'), 'Two cache items were created in all backends.');

    // Invalidate test_tag of value 1. This should invalidate both entries.
    $this->chain
      ->deleteTags(array(
      'test_tag' => array(
        1,
      ),
    ));
    $this
      ->assertIdentical(FALSE, $this->firstBackend
      ->get('test_cid_clear1') && $this->firstBackend
      ->get('test_cid_clear2') && $this->secondBackend
      ->get('test_cid_clear1') && $this->secondBackend
      ->get('test_cid_clear2') && $this->thirdBackend
      ->get('test_cid_clear1') && $this->thirdBackend
      ->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.');

    // Create three cache entries with a mix of tags and tag values.
    $this->chain
      ->set('test_cid_clear1', 'foo', CacheBackendInterface::CACHE_PERMANENT, array(
      'test_tag' => array(
        1,
      ),
    ));
    $this->chain
      ->set('test_cid_clear2', 'foo', CacheBackendInterface::CACHE_PERMANENT, array(
      'test_tag' => array(
        2,
      ),
    ));
    $this->chain
      ->set('test_cid_clear3', 'foo', CacheBackendInterface::CACHE_PERMANENT, array(
      'test_tag_foo' => array(
        3,
      ),
    ));
    $this
      ->assertNotIdentical(FALSE, $this->firstBackend
      ->get('test_cid_clear1') && $this->firstBackend
      ->get('test_cid_clear2') && $this->firstBackend
      ->get('test_cid_clear3') && $this->secondBackend
      ->get('test_cid_clear1') && $this->secondBackend
      ->get('test_cid_clear2') && $this->secondBackend
      ->get('test_cid_clear3') && $this->thirdBackend
      ->get('test_cid_clear1') && $this->thirdBackend
      ->get('test_cid_clear2') && $this->thirdBackend
      ->get('test_cid_clear3'), 'Three cached items were created in all backends.');
    $this->chain
      ->deleteTags(array(
      'test_tag_foo' => array(
        3,
      ),
    ));
    $this
      ->assertNotIdentical(FALSE, $this->firstBackend
      ->get('test_cid_clear1') && $this->firstBackend
      ->get('test_cid_clear2') && $this->secondBackend
      ->get('test_cid_clear1') && $this->secondBackend
      ->get('test_cid_clear2') && $this->thirdBackend
      ->get('test_cid_clear1') && $this->thirdBackend
      ->get('test_cid_clear2'), 'Cached items not matching the tag were not cleared from any of the backends.');
    $this
      ->assertIdentical(FALSE, $this->firstBackend
      ->get('test_cid_clear3') && $this->secondBackend
      ->get('test_cid_clear3') && $this->thirdBackend
      ->get('test_cid_clear3'), 'Cached item matching the tag was removed from all backends.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackendChainImplementationUnitTest::$chain protected property Chain that will be heavily tested.
BackendChainImplementationUnitTest::$firstBackend protected property First backend in the chain.
BackendChainImplementationUnitTest::$secondBackend protected property Second backend in the chain.
BackendChainImplementationUnitTest::$thirdBackend protected property Third backend in the chain.
BackendChainImplementationUnitTest::getInfo public static function
BackendChainImplementationUnitTest::setUp public function Sets up unit test environment. Overrides UnitTestBase::setUp
BackendChainImplementationUnitTest::testDelete public function Test that delete will propagate.
BackendChainImplementationUnitTest::testDeleteAllPropagation public function Test that the delete all operation is propagated to all backends in the chain.
BackendChainImplementationUnitTest::testDeleteTagsPropagation public function Test that the delete tags operation is propagated to all backends in the chain.
BackendChainImplementationUnitTest::testGet public function Test the get feature.
BackendChainImplementationUnitTest::testGetHasPropagated public function Ensure get values propagation to previous backends.
BackendChainImplementationUnitTest::testGetMultiple public function Test the get multiple feature.
BackendChainImplementationUnitTest::testGetMultipleHasPropagated public function Ensure get multiple values propagation to previous backends.
BackendChainImplementationUnitTest::testNotEmptyIfOneBackendHasTheKey public function Test that the chain is not empty when at least one backend has data.
BackendChainImplementationUnitTest::testSet public function Test that set will propagate.
TestBase::$assertions protected property Assertions thrown in that test case.
TestBase::$databasePrefix protected property The database prefix of this test run.
TestBase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
TestBase::$originalPrefix protected property The original database prefix when running inside Simpletest.
TestBase::$results public property Current results of this test case.
TestBase::$setup protected property Flag to indicate whether the test has been set up.
TestBase::$setupDatabasePrefix protected property
TestBase::$setupEnvironment protected property
TestBase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
TestBase::$testId protected property The test run ID.
TestBase::$timeLimit protected property Time limit for the test.
TestBase::$verbose protected property TRUE if verbose debugging is enabled.
TestBase::$verboseClassName protected property Safe class name for use in verbose output filenames.
TestBase::$verboseDirectory protected property Directory where verbose output files are put.
TestBase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
TestBase::$verboseId protected property Incrementing identifier for verbose output filenames.
TestBase::assert protected function Internal helper: stores the assert.
TestBase::assertEqual protected function Check to see if two values are equal.
TestBase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
TestBase::assertIdentical protected function Check to see if two values are identical.
TestBase::assertIdenticalObject protected function Checks to see if two objects are identical.
TestBase::assertNotEqual protected function Check to see if two values are not equal.
TestBase::assertNotIdentical protected function Check to see if two values are not identical.
TestBase::assertNotNull protected function Check to see if a value is not NULL.
TestBase::assertNull protected function Check to see if a value is NULL.
TestBase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
TestBase::changeDatabasePrefix protected function Changes the database connection to the prefixed one.
TestBase::checkRequirements protected function Checks the matching requirements for Test. 3
TestBase::deleteAssert public static function Delete an assertion record by message ID.
TestBase::error protected function Fire an error assertion. 1
TestBase::errorHandler public function Handle errors during test runs.
TestBase::exceptionHandler protected function Handle exceptions.
TestBase::fail protected function Fire an assertion that is always negative.
TestBase::filePreDeleteCallback public static function Ensures test files are deletable within file_unmanaged_delete_recursive().
TestBase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
TestBase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
TestBase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
TestBase::insertAssert public static function Store an assertion from outside the testing context.
TestBase::pass protected function Fire an assertion that is always positive.
TestBase::prepareDatabasePrefix protected function Generates a database prefix for running tests.
TestBase::prepareEnvironment protected function Prepares the current environment for running the test.
TestBase::randomName public static function Generates a random string containing letters and numbers.
TestBase::randomObject public static function Generates a random PHP object.
TestBase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
TestBase::rebuildContainer protected function Rebuild drupal_container().
TestBase::run public function Run all tests in this class.
TestBase::tearDown protected function Deletes created files, database tables, and reverts all environment changes. 10
TestBase::verbose protected function Logs verbose message in a text file.
UnitTestBase::$configDirectories protected property
UnitTestBase::__construct function Constructor for UnitTestBase. Overrides TestBase::__construct 6