You are here

function VariableStoreTestCase::testVariableStoreAPI in Variable 7.2

Same name and namespace in other branches
  1. 7 variable_store/variable_store.test \VariableStoreTestCase::testVariableStoreAPI()

Test that all core modules can be enabled, disabled and uninstalled.

File

variable_store/variable_store.test, line 32
Tests for variable.module.

Class

VariableStoreTestCase
Helper class for module test cases.

Code

function testVariableStoreAPI() {

  // Store two values for the 'example' realm.
  $realm = 'example';
  $store = array(
    'first' => 'My first test site',
    'second' => 'My second test site',
  );
  $values = array(
    'default' => 'Drupal',
  ) + $store;
  foreach ($store as $key => $value) {
    variable_store_set($realm, $key, 'site_name', $value);
  }

  // Check we've got stored the right value for each realm.
  foreach ($store as $key => $value) {
    $this
      ->assertEqual(variable_store_get($realm, $key, 'site_name'), $value, 'Variable has been saved to the store.');

    // Check the value is displayed with example page.
    $this
      ->drupalGet('variable/realm/' . $realm . '/' . $key);
    $this
      ->assertText($value, "The right site name is displayed for realm {$realm}:{$key}");
  }

  // Load realms from store and set them as different realm.
  foreach ($store as $key => $value) {
    $variables = variable_store($realm, $key);
    variable_realm_add($realm, $key, $variables);
  }

  // Switch realms and get the right values.
  foreach ($values as $key => $value) {

    // Check value before setting realm
    $this
      ->assertEqual(variable_realm_get($realm, $key, 'site_name', 'Drupal'), $value, "We get the right value for realm {$realm}:{$key}");
    variable_realm_switch($realm, $key);
    $this
      ->assertEqual(variable_get_value('site_name'), $value, "We get the right value with Variable API after switching to realm {$realm}:{$key}");
    $this
      ->assertEqual(variable_get('site_name', 'Drupal'), $value, "We get the right value with Drupal API after switching to realm {$realm}:{$key}");
  }

  // Delete variable and check it has been deleted, first from realms and then from store.
  variable_delete('site_name');

  // Check we've got stored the right value for each realm.
  foreach ($store as $key => $value) {
    $this
      ->assertFalse(variable_store_get($realm, $key, 'site_name'), "Variable has been deleted from the realm for {$realm}:{$key}.");
    $this
      ->assertFalse(variable_store_get($realm, $key, 'site_name'), "Variable has been deleted from the store for {$realm}:{$key}.");
  }

  // Finally check variable has returned to default value. Use any of the realms.
  variable_realm_switch($realm, 'first');
  $this
    ->assertEqual($value = variable_get_value('site_name'), 'Drupal', "We get the right value with Variable API after deleting the variable: {$value}");
  $this
    ->assertFalse($value = variable_get('site_name'), "We get the right value with Drupal API after deleting the variable: {$value}");
}