You are here

protected function AuthcacheP13nTestDatabaseKeyValueStore::testSet in Authenticated User Page Caching (Authcache) 7.2

Cover AuthcacheP13nDatabaseKeyValueStore::set().

File

modules/authcache_p13n/tests/authcache_p13n.db-kv-store.test, line 86
Test case for database key-value store.

Class

AuthcacheP13nTestDatabaseKeyValueStore
Test database key-value store.

Code

protected function testSet() {

  // Insert data.
  $this->store1
    ->set('k1', 'v1');
  $this->store1
    ->set('k2', array(
    'some',
    'stuff',
  ));
  $this->store1
    ->set('k3', (object) array(
    'a' => 1,
    'b' => 2,
  ));
  $this->store2
    ->set('k1', 'same key, other store');
  $expect = array(
    array(
      'name' => 'k1',
      'collection' => 'store1',
      'value' => serialize('v1'),
    ),
    array(
      'name' => 'k2',
      'collection' => 'store1',
      'value' => serialize(array(
        'some',
        'stuff',
      )),
    ),
    array(
      'name' => 'k3',
      'collection' => 'store1',
      'value' => serialize((object) array(
        'a' => 1,
        'b' => 2,
      )),
    ),
    array(
      'name' => 'k1',
      'collection' => 'store2',
      'value' => serialize('same key, other store'),
    ),
  );
  $rows = db_select('authcache_p13n_key_value', 'kv')
    ->fields('kv', array(
    'name',
    'collection',
    'value',
  ))
    ->orderBy('collection')
    ->orderBy('name')
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);
  $this
    ->assertEqual($expect, $rows, 'Database records are properly created');

  // Replace data.
  $this->store1
    ->set('k3', 'replaced');
  $expect = array(
    array(
      'name' => 'k1',
      'collection' => 'store1',
      'value' => serialize('v1'),
    ),
    array(
      'name' => 'k2',
      'collection' => 'store1',
      'value' => serialize(array(
        'some',
        'stuff',
      )),
    ),
    array(
      'name' => 'k3',
      'collection' => 'store1',
      'value' => serialize('replaced'),
    ),
    array(
      'name' => 'k1',
      'collection' => 'store2',
      'value' => serialize('same key, other store'),
    ),
  );
  $rows = db_select('authcache_p13n_key_value', 'kv')
    ->fields('kv', array(
    'name',
    'collection',
    'value',
  ))
    ->orderBy('collection')
    ->orderBy('name')
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);
  $this
    ->assertEqual($expect, $rows, 'Database records are properly replaced');

  // Remove test data.
  db_delete('authcache_p13n_key_value')
    ->execute();
}