You are here

public function ServicesResourceWebformTests::testEndpointSubmission in Webform Service 7.4

Test creating submissions on a webform.

File

tests/ServicesResourceWebformTests.test, line 387

Class

ServicesResourceWebformTests
Run test cases for the endpoint with no authentication turned on.

Code

public function testEndpointSubmission() {

  // Create and log in our privileged user.
  $this->privilegedUser = $this
    ->drupalCreateUser(array(
    'administer services',
    'bypass node access',
  ));
  $this
    ->drupalLogin($this->privilegedUser);

  // Create the form.
  $components = $this->form_components;
  $form = $this
    ->createForm($components);
  $webform = webform_service_resource_load($form->uuid);

  // Create and login a new user.
  $this
    ->drupalLogin($this
    ->drupalCreateUser(array(
    'access own webform submissions',
    'edit own webform submissions',
    'delete own webform submissions',
    'access own webform results',
  )));

  // Set the endpoint...
  $endpoint = $this->endpoint->path . '/submission';
  $submissions = array();

  // Add 10 submissions.
  for ($i = 0; $i < 10; $i++) {
    $rand = rand(0, 1000000);
    $data1 = array(
      'values' => array(
        'first' . $rand,
      ),
    );
    $data2 = array(
      'values' => array(
        'last' . $rand,
      ),
    );
    $data3 = array(
      'values' => array(
        'email' . $rand . '@example.com',
      ),
    );
    $response = $this
      ->servicesPost($endpoint, array(
      'webform' => $webform->uuid,
      'submission' => array(
        'data' => array(
          1 => $data1,
          2 => $data2,
          3 => $data3,
        ),
      ),
    ));
    $response = $response['body'];
    $this
      ->assertTrue($response['data'][1]['values'] == $data1['values']);
    $this
      ->assertTrue($response['data'][2]['values'] == $data2['values']);
    $this
      ->assertTrue($response['data'][3]['values'] == $data3['values']);
  }

  // Now get some submissions and update them.
  $endpoint = $this->endpoint->path . '/webform/' . $webform->uuid . '/submissions';
  $indexResponse = $this
    ->servicesGet($endpoint);
  $indexResponse = $indexResponse['body'];

  // Iterate through 3 submissions and update them to make sure they update.
  for ($index = 2; $index < 5; $index++) {

    // Get the resource for this index.
    $resource = $indexResponse[$index];

    // Set the crud endpoint.
    $crud = $this->endpoint->path . '/submission/' . $resource['uuid'];

    // The get endpoint.
    $getResponse = $this
      ->servicesGet($crud);
    $getResponse = $getResponse['body'];
    $this
      ->assertTrue($getResponse['sid'] == $resource['sid']);

    // Update the resource.
    $data1 = array(
      'values' => array(
        'First ' . $index,
      ),
    );
    $data2 = array(
      'values' => array(
        'Last ' . $index,
      ),
    );
    $data3 = array(
      'values' => array(
        'email' . $index . '@email.com',
      ),
    );
    $resource['data'] = array(
      1 => $data1,
      2 => $data2,
      3 => $data3,
    );
    $updateResponse = $this
      ->servicesPut($crud, $resource);
    $updateResponse = $updateResponse['body'];
    $this
      ->assertTrue($updateResponse['data'][1]['values'] == $data1['values']);
    $this
      ->assertTrue($updateResponse['data'][2]['values'] == $data2['values']);
    $this
      ->assertTrue($updateResponse['data'][3]['values'] == $data3['values']);
  }
}