You are here

public function AuthcacheP13nTestObjectResourcePreprocessorCase::testDefaultPreprocessors in Authenticated User Page Caching (Authcache) 7.2

Covers AuthcacheP13nObjectResourcePreprocessor::preprocess().

File

modules/authcache_p13n/tests/authcache_p13n.object-resource-preprocessor.test, line 59
Define test cases for object factory.

Class

AuthcacheP13nTestObjectResourcePreprocessorCase
Tests for object factory.

Code

public function testDefaultPreprocessors() {
  $definition = array();
  $preproc = new AuthcacheP13nObjectResourcePreprocessor(AuthcacheP13nObjectResourcePreprocessor::defaultPreprocessors());
  $resources = $preproc
    ->preprocess($definition);
  $this
    ->assertEqual(array(), $resources, t('An empty definition should result in no resources'));

  // Test scalar values. We cannot test for string from within a unit test
  // though, because that would trigger the autoloader.
  $definition = array(
    'an int' => 42,
    'a float' => 3.14,
    'a boolean' => TRUE,
  );
  $expect_resources = array(
    'an int' => array(
      '#type' => 'value',
      '#value' => 42,
    ),
    'a float' => array(
      '#type' => 'value',
      '#value' => 3.14,
    ),
    'a boolean' => array(
      '#type' => 'value',
      '#value' => TRUE,
    ),
  );
  $resources = $preproc
    ->preprocess($definition);
  $this
    ->assertEqual($expect_resources, $resources, t('Scalars should result in scalar value-resources'));

  // Test arrays and objects.
  $definition = array(
    'an array' => array(
      'hello' => 'world',
    ),
    'an object' => (object) array(
      'some' => 'prop',
    ),
  );
  $expect_resources = array(
    'an array' => array(
      '#type' => 'value',
      '#value' => array(
        'hello' => 'world',
      ),
    ),
    'an object' => array(
      '#type' => 'value',
      '#value' => (object) array(
        'some' => 'prop',
      ),
    ),
  );
  $resources = $preproc
    ->preprocess($definition);
  $this
    ->assertEqual($expect_resources, $resources, t('Compound types should result in value-resources'));

  // Add #type when #value is in array.
  $definition = array(
    'an implicit value' => array(
      '#value' => 42,
    ),
    'an implicit func' => array(
      '#func' => 'strlen',
    ),
    'an implicit class' => array(
      '#class' => 'DummyClass',
    ),
    'an implicit collection' => array(
      '#collection' => 'a collection',
    ),
  );
  $expect_resources = array(
    'an implicit value' => array(
      '#value' => 42,
      '#type' => 'value',
    ),
    'an implicit func' => array(
      '#func' => 'strlen',
      '#type' => 'func',
    ),
    'an implicit class' => array(
      '#class' => 'DummyClass',
      '#type' => 'class',
    ),
    'an implicit collection' => array(
      '#collection' => 'a collection',
      '#type' => 'collection',
    ),
  );
  $resources = $preproc
    ->preprocess($definition);
  $this
    ->assertEqual($expect_resources, $resources, t('A #type => value should be added when #value, #func or #class is present'));

  // Pass through arrays when #type is one of 'value', 'func', 'class'
  // or 'collection'.
  $definition = array(
    'an incomplete value' => array(
      '#type' => 'value',
    ),
    'an incomplete func' => array(
      '#type' => 'func',
    ),
    'an incomplete class' => array(
      '#type' => 'class',
    ),
    'an incomplete collection' => array(
      '#type' => 'collection',
    ),
  );
  $resources = $preproc
    ->preprocess($definition);
  $this
    ->assertEqual($definition, $resources, t('Resources should not be processed when type is one of value, func, class'));
}