From 1b5e3bef4511272315ce7d49697444a814e1ff6d Mon Sep 17 00:00:00 2001
From: Kyle Browning <kylebrowning@me.com>
Date: Wed, 13 Apr 2011 13:36:53 -0700
Subject: [PATCH] Variable Delete patch for system resource

---
 resources/system_resource.inc                     |   52 ++++++++-------------
 tests/functional/ServicesResourceSystemTests.test |   20 ++++++++
 tests/services.test                               |    3 +
 3 files changed, 42 insertions(+), 33 deletions(-)

diff --git a/resources/system_resource.inc b/resources/system_resource.inc
index b7d2a34..7748309 100644
--- a/resources/system_resource.inc
+++ b/resources/system_resource.inc
@@ -17,7 +17,7 @@ function _system_resource_definition() {
         'get_variable' => array(
           'help'   => t('Returns the value of a system variable using variable_get().'),
           'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/system_resource'),
-          'callback' => '_system_resource_get_variable',
+          'callback' => 'variable_get',
           'access arguments' => array('get a system variable'),
           'access arguments append' => FALSE,
           'args' => array(
@@ -30,7 +30,7 @@ function _system_resource_definition() {
             ),
             array(
               'name' => 'default',
-              'optional' => FALSE,
+              'optional' => TRUE,
               'source' => 'data',
               'description' => t('The default value to use if this variable has never been set.'),
               'type' => 'string',
@@ -40,7 +40,7 @@ function _system_resource_definition() {
         'set_variable' => array(
           'help'   => t('Sets the value of a system variable using variable_set().'),
           'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/system_resource'),
-          'callback' => '_system_resource_set_variable',
+          'callback' => 'variable_set',
           'access arguments' => array('set a system variable'),
           'access arguments append' => FALSE,
           'args' => array(
@@ -60,6 +60,22 @@ function _system_resource_definition() {
             ),
           ),
         ),
+        'del_variable' => array(
+          'help'   => t('Deletes a system variable using variable_del().'),
+          'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/system_resource'),
+          'callback' => 'variable_del',
+          'access arguments' => array('set a system variable'),
+          'access arguments append' => FALSE,
+          'args' => array(
+            array(
+              'name' => 'name',
+              'optional' => FALSE,
+              'source' => 'data',
+              'description' => t('The name of the variable to delete.'),
+              'type' => 'string',
+            ),
+          ),
+        ),
       ),
     ),
   );
@@ -81,33 +97,3 @@ function _system_resource_connect() {
   return $return;
 }
 
-/**
- * Services implementation of variable_get().
- *
- * @param $name
- *   The name of the variable to return.
- * @param $default
- *   The value to use if the variable has never been set.
- *
- * @return
- *   The value of the variable.
- *
- * @see variable_get()
- */
-function _system_resource_get_variable($name, $default) {
-  return variable_get($name, $default);
-}
-
-/**
- * Services implementation of variable_set().
- *
- * @param $name
- *   The name of the variable to set.
- * @param $value
- *   The value to set this variable to.
- *
- * @see variable_set()
- */
-function _system_resource_set_variable($name, $value) {
-  variable_set($name, $value);
-}
diff --git a/tests/functional/ServicesResourceSystemTests.test b/tests/functional/ServicesResourceSystemTests.test
index 5981208..20839c9 100644
--- a/tests/functional/ServicesResourceSystemTests.test
+++ b/tests/functional/ServicesResourceSystemTests.test
@@ -99,4 +99,24 @@ class ServicesResourceSystemTests extends ServicesWebtestCase {
 
     $this->assertEqual($value, $variable, t('Variable set value.'), 'SystemResource: set_variable');
   }
+
+  /**
+   * Test set_variable method.
+   */
+  function testSystemDelVariable() {
+    $path = $this->endpoint->path;
+
+    // Set a random variable.
+    $name = $this->randomName();
+    $value = $this->randomString();
+    variable_set($name, $value);
+
+    // Delete the variable via del_variable.
+    $response = $this->servicesPost($path . '/system/del_variable', array('name' => $name));
+
+    // We can't use variable_get as variables get cached to global variable.
+    $newvalue = $this->randomString();
+    $response = $this->servicesPost($path . '/system/get_variable', array('name' => $name, 'default' => $newvalue));
+    $this->assertEqual($newvalue, $response['body'], t('Variable deleted.'), 'SystemResource: get_variable');
+  }
 }
diff --git a/tests/services.test b/tests/services.test
index 27d6098..fbec159 100644
--- a/tests/services.test
+++ b/tests/services.test
@@ -213,6 +213,9 @@ class ServicesWebTestCase extends DrupalWebTestCase {
           'set_variable' => array(
             'enabled' => 1,
           ),
+          'del_variable' => array(
+            'enabled' => 1,
+          ),
         ),
       ),
       'taxonomy_term' => array(
-- 
1.7.3.4

