You are here

public function SpacesContextControllerTestCase::test in Spaces 6.3

Same name and namespace in other branches
  1. 7.3 tests/spaces.test \SpacesContextControllerTestCase::test()
  2. 7 tests/spaces.test \SpacesContextControllerTestCase::test()

Test override inheritance of variable controller.

File

tests/spaces.test, line 129

Class

SpacesContextControllerTestCase
Unit tests for context controller

Code

public function test() {
  ctools_include('export');

  // Create a test context.
  $context = ctools_export_new_object('context');
  $context->name = 'testcontext';
  $context->conditions = array(
    'path' => array(
      'values' => array(
        '<front>',
      ),
    ),
  );
  $context->reactions = array();
  $saved = context_save($context);
  $this
    ->assertTrue($saved, "Context 'testcontext' saved.");

  // Create a test preset.
  $preset = ctools_export_new_object('spaces_presets');
  $preset->name = 'testpreset';
  $preset->title = 'Test preset';
  $preset->space_type = 'user';
  $preset->value = array(
    'context' => array(
      'testcontext:condition:path' => array(
        'values' => array(
          'node',
        ),
      ),
    ),
  );
  spaces_preset_save($preset);

  // Force a cache clear.
  spaces_types(true);
  $info =& ctools_static('ctools_plugin_info', array());
  $info = array();
  $space = spaces_load('user', 0);
  if ($space == FALSE) {
    $this
      ->fail('Could not load user space');

    // return and prevent the test from going haywire.
    return;
  }
  else {
    $this
      ->pass('Loaded user space');
  }

  // Activate space.
  $space
    ->activate();

  // Original
  $this
    ->assertTrue($space->controllers->context
    ->get('testcontext:condition:path') === array(
    'values' => array(
      '<front>',
    ),
  ), t('Inheritance (Original)'));

  // Original > Preset
  $space->controllers->variable
    ->set('spaces_preset_user', 'testpreset');
  $space
    ->init_overrides();
  $this
    ->assertTrue($space->controllers->context
    ->get('testcontext:condition:path') === array(
    'values' => array(
      'node',
    ),
  ), t('Inheritance (Original > Preset)'));

  // Original > Preset > Space
  $space->controllers->context
    ->set('testcontext:condition:path', array(
    'values' => array(
      'user',
    ),
  ));
  $this
    ->assertTrue($space->controllers->context
    ->get('testcontext:condition:path') === array(
    'values' => array(
      'user',
    ),
  ), t('Inheritance (Original > Preset > Space)'));

  // Original > Space
  $space->controllers->variable
    ->del('spaces_preset_user');
  $space
    ->init_overrides();
  $this
    ->assertTrue($space->controllers->context
    ->get('testcontext:condition:path') === array(
    'values' => array(
      'user',
    ),
  ), t('Inheritance (Original > Space)'));

  // Put the preset back
  $space->controllers->variable
    ->set('spaces_preset_user', 'testpreset');
  $space
    ->init_overrides();

  // Specific environment gets
  $this
    ->assertTrue($space->controllers->context
    ->get('testcontext:condition:path', 'original') === array(
    'values' => array(
      '<front>',
    ),
  ), t('Environment (Original)'));
  $this
    ->assertTrue($space->controllers->context
    ->get('testcontext:condition:path', 'preset') === array(
    'values' => array(
      'node',
    ),
  ), t('Environment (Preset)'));
  $this
    ->assertTrue($space->controllers->context
    ->get('testcontext:condition:path', 'space') === array(
    'values' => array(
      'user',
    ),
  ), t('Environment (Space)'));

  // Clean up.
  context_delete('testcontext');
  spaces_delete('user', 0);
  spaces_preset_delete(spaces_preset_load('testpreset'));
}