variable_store.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @file
  4. * Tests for variable.module.
  5. */
  6. /**
  7. * Helper class for module test cases.
  8. */
  9. class VariableStoreTestCase extends DrupalWebTestCase {
  10. protected $admin_user;
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Test Variable store and realms',
  14. 'description' => 'Exercise the Variable API, default values, save and delete variables, etc.',
  15. 'group' => 'Variable',
  16. );
  17. }
  18. function setUp() {
  19. parent::setUp('variable', 'variable_realm', 'variable_store', 'variable_example');
  20. $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer site configuration'));
  21. $this->drupalLogin($this->admin_user);
  22. }
  23. /**
  24. * Test that all core modules can be enabled, disabled and uninstalled.
  25. */
  26. function testVariableStoreAPI() {
  27. // Store two values for the 'example' realm.
  28. $realm = 'example';
  29. $store = array(
  30. 'first' => 'My first test site',
  31. 'second' => 'My second test site',
  32. );
  33. $values = array(
  34. 'default' => 'Drupal',
  35. ) + $store;
  36. foreach ($store as $key => $value) {
  37. variable_store_set($realm, $key, 'site_name', $value);
  38. }
  39. // Check we've got stored the right value for each realm.
  40. foreach ($store as $key => $value) {
  41. $this->assertEqual(variable_store_get($realm, $key, 'site_name'), $value, 'Variable has been saved to the store.');
  42. // Check the value is displayed with example page.
  43. $this->drupalGet('variable/realm/' . $realm . '/' . $key);
  44. $this->assertText($value, "The right site name is displayed for realm $realm:$key");
  45. }
  46. // Load realms from store and set them as different realm.
  47. foreach ($store as $key => $value) {
  48. $variables = variable_store($realm, $key);
  49. variable_realm_add($realm, $key, $variables);
  50. }
  51. // Switch realms and get the right values.
  52. foreach ($values as $key => $value) {
  53. // Check value before setting realm
  54. $this->assertEqual(variable_realm_get($realm, $key, 'site_name', 'Drupal'), $value, "We get the right value for realm $realm:$key");
  55. variable_realm_switch($realm, $key);
  56. $this->assertEqual(variable_get_value('site_name'), $value, "We get the right value with Variable API after switching to realm $realm:$key");
  57. $this->assertEqual(variable_get('site_name', 'Drupal'), $value, "We get the right value with Drupal API after switching to realm $realm:$key");
  58. }
  59. // Delete variable and check it has been deleted, first from realms and then from store.
  60. variable_delete('site_name');
  61. // Check we've got stored the right value for each realm.
  62. foreach ($store as $key => $value) {
  63. $this->assertFalse(variable_store_get($realm, $key, 'site_name'), "Variable has been deleted from the realm for $realm:$key.");
  64. $this->assertFalse(variable_store_get($realm, $key, 'site_name'), "Variable has been deleted from the store for $realm:$key.");
  65. }
  66. // Finally check variable has returned to default value. Use any of the realms.
  67. variable_realm_switch($realm, 'first');
  68. $this->assertEqual($value = variable_get_value('site_name'), 'Drupal', "We get the right value with Variable API after deleting the variable: $value");
  69. $this->assertFalse($value = variable_get('site_name'), "We get the right value with Drupal API after deleting the variable: $value");
  70. }
  71. }