You are here

public function ServicesClientWebTestCase::testServicesClientProcessing in Services Client 7.2

File

tests/services_client.test, line 447
Tests for the Administration menu module.

Class

ServicesClientWebTestCase
Base class for all administration menu web test cases.

Code

public function testServicesClientProcessing() {

  // Login admin user.
  $this
    ->drupalLogin($this->admin);
  $content_type = $this
    ->drupalCreateContentType(array(
    'type' => 'post',
    'name' => 'Post',
  ));
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'post',
    'title' => 'Test post',
  ));
  $result = services_client_process_events('save', $node, 'node');
  $this
    ->assertIdentical($result, array(), "No events are triggered by default.");
  $event = $this
    ->createSCEvent();

  // Add new property condition.
  $conditions['status'] = $this
    ->addEventCondition($event, 'ServicesClientPropertyCondition', array(
    'property' => 'status',
    'condition' => 'equals',
    'value' => '1',
  ));
  $conditions['status'] = $this
    ->addEventCondition($event, 'ServicesClientPropertyCondition', array(
    'property' => 'type',
    'condition' => 'equals',
    'value' => 'post',
  ));

  // Add uid mapping.
  $mapping['uid'] = $this
    ->addEventMapping($event, 'ServicesClientPropertyReader', array(
    'property' => 'uid',
  ), 'ServicesClientPropertyFormatter', array(
    'property' => 'uid',
  ));
  $reader = array(
    'field' => 'body',
    'property' => 'value',
  );
  $formatter = array(
    'field' => 'field_body',
    'property' => 'value',
  );
  $mapping['body'] = $this
    ->addEventMapping($event, 'ServicesClientFieldReader', $reader, 'ServicesClientFieldFormatter', $formatter);
  $formatter = array(
    'field' => 'field_body_d6',
    'property' => 'value',
  );
  $mapping['body_d6'] = $this
    ->addEventMapping($event, 'ServicesClientFieldReader', $reader, 'ServicesClientFieldD6Formatter', $formatter);
  $this
    ->saveEventConfiguration($event, array(), TRUE);
  $result = services_client_process_events('save', $node, 'node');
  $this
    ->assertIdentical($result, array(), "Disabled events are not triggered by default.");

  // Enable configured event.
  $this
    ->enabledEvent($event);
  $result = services_client_process_events('save', $node, 'node');
  $this
    ->assertIdentical(count($result), 1, "One event was triggered automatically.");
  $result = reset($result);
  $this
    ->assertIdentical(get_class($result), 'ServicesClientEventResult', "Syncing operation result is returned in correct format");
  $this
    ->assertIdentical($result
    ->getEntityId(), $node->nid, "Response object extracts entity id correctly.");
  $this
    ->assertIdentical($result
    ->success(), FALSE, "Failed event is reporting correct result status.");
  $this
    ->assertTrue(!empty($result->object), "Sync event created non-empty object.");
  $this
    ->assertTrue(!empty($result->object->_services_client), 'Control data was correctly set to mapped object.');
  $this
    ->assertIdentical($result->object->uid, $node->uid, "Node uid was mapped correctly.");
  $this
    ->assertIdentical($result->object->field_body[LANGUAGE_NONE][0]['value'], $node->body[LANGUAGE_NONE][0]['value'], "Body field was mapped correclty with field mapper.");
  $this
    ->assertIdentical($result->object->field_body_d6[0]['value'], $node->body[LANGUAGE_NONE][0]['value'], "Body field was mapped correclty with field mapper.");

  // Test looping control
  $node->_services_client['origin'] = 'remote_site';
  $node->_services_client['visted'] = array(
    'remote_site',
  );
  $handler = $event
    ->getHandler();
  $result = $handler
    ->setEntity($node)
    ->execute();
  $this
    ->assertIdentical($result
    ->success(), FALSE, "Looping event is reporting correct result status.");
  $this
    ->assertIdentical($result->error_type, ServicesClientErrorType::LOOP, "Loop error type is reported.");

  // Test control data queueing.
  $this
    ->clearQueue();
  $result = services_client_process_events('save', $node, 'node');
  $this
    ->assertIdentical($result, array(), "No events were processed when node has control data.");
  $item = $this
    ->getQueueItem();
  $this
    ->assertIdentical($node->nid, $item->data['entity']->nid, "Processed entity was queued when contain controlling data");
  $this
    ->clearQueue();

  // Bypass queue processing.
  $node->_services_client['bypass_queue'] = TRUE;
  $result = services_client_process_events('save', $node, 'node');
  $this
    ->assertTrue(!empty($result), "Event was processed directly if bypas_queue flag was present.");
  $result = reset($result);
  $this
    ->assertIdentical($result
    ->getEntityId(), $node->nid, "Response object extracts entity id correctly.");
  $this
    ->assertIdentical($result
    ->success(), FALSE, "Failed event is reporting correct result status.");
  $item = $this
    ->getQueueItem();
  $this
    ->assertTrue(empty($item), "No object was queued if bypass_queue is present.");

  // Test auto queueing
  $this
    ->saveEventConfiguration($event, array(
    'queue' => 1,
  ), TRUE);
  unset($node->_services_client);
  $result = services_client_process_events('save', $node, 'node');
  $this
    ->assertIdentical($result, array(), "No events were processed when force queue is enabled.");
  $item = $this
    ->getQueueItem();
  $this
    ->assertIdentical($node->nid, $item->data['entity']->nid, "Processed entity was queued when force queue is enabled.");
  $this
    ->clearQueue();

  // Test non-triggered events.
  $this
    ->saveEventConfiguration($event, array(
    'queue' => FALSE,
    'auto_triggered' => FALSE,
  ), TRUE);
  $result = services_client_process_events('save', $node, 'node');
  $this
    ->assertIdentical($result, array(), "No events were processed when auto trigger is off.");
  $item = $this
    ->getQueueItem();
  $this
    ->assertTrue(empty($item), "No queue item was created when auto trigger is turned off.");
}