saveNewEndpoint() is used by setUp() in test classes as a helper to save a new endpoint

However, it hardcodes the list of resources it sets on the new endpoint. This means that it's useful for most of the test classes in Services module -- but not all. And it's useless for any contrib modules that want to use ServicesWebTestCase as a parent class to test their resources.

I suggest adding a parameter to saveNewEndpoint() which would take an array of resources, eg:

saveNewEndpoint(array('node', 'taxonomy'))

In the function, we add 'system' and 'user' as those are always needed, and then call services_get_resources() to figure out all the sub-components (operations, targeted actions etc) to enable.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim’s picture

I'll make this into a patch later -- this is just proof of concept code:

    // Add the resources we always need.
    $resources[] = 'system';
    $resources[] = 'user';

    // Get all defined resources to fill in the requested resource types.
    $defined_resources = services_get_resources();

    $endpoint->resources = array();
    foreach ($resources as $resource_name) {
      $endpoint->resources[$resource_name] = array();

      // Now work through the operations / actions / etc for each resource.
      foreach ($defined_resources[$resource_name] as $service_type => $service_info) {
        $endpoint->resources[$resource_name][$service_type] = array();

        // Add the arrays that enable each service.
        foreach (array_keys($service_info) as $service_name) {
          $endpoint->resources[$resource_name][$service_type][$service_name] = array(
            'enabled' => 1,
          );
        }
      }
    }

This allows callers to just say:

    $this->endpoint = $this->saveNewEndpoint(array('taxonomy'));

which improves DX a fair bit :)

joachim’s picture

Status: Active » Needs review
FileSize
18.75 KB

Patch, which is cumulative on #2091169: pointless overrides of populateEndpointFAPI() (and hence will probably fail without that -- or at least cause a notice due to the lack of a 'title' property in the array from populateEndpointFAPI() in the base class).

Status: Needs review » Needs work

The last submitted patch, 2089445.services.saveNewEndpoint-parameter.patch, failed testing.

joachim’s picture

Status: Needs work » Needs review
FileSize
19.13 KB

Reroll. Should pass, as I've added the 'title' array key in the parent implementation of populateEndpointFAPI().

(Which means #2091169: pointless overrides of populateEndpointFAPI() will need a reroll after this gets in.)

Status: Needs review » Needs work

The last submitted patch, 2089445-4.services.saveNewEndpoint-parameter.patch, failed testing.

kylebrowning’s picture

Issue summary: View changes

Is this still being worked on?

joachim’s picture

I'll try and find time to reroll this. It would make life a lot easier for contrib modules that use Services in their tests.

kylebrowning’s picture

Status: Needs work » Postponed