Пример #1
0
 /**
  * Test subscribing and then receiving data:
  */
 public function testSubscription()
 {
     // 1: Request to subscribe:
     $hookName = 'ContactsCreate';
     $hook = array('event' => 'RecordCreateTrigger', 'target_url' => TEST_WEBROOT_URL . '/api2HooksTest.php?name=' . $hookName);
     $ch = $this->getCurlHandle('POST', array('{suffix}' => ''), 'admin', $hook, array(CURLOPT_HEADER => 1));
     $response = curl_exec($ch);
     $this->assertResponseCodeIs(201, $ch, VERBOSE_MODE ? $response : '');
     $trigger = ApiHook::model()->findByAttributes($hook);
     // 2. Create a contact
     $contact = array('firstName' => 'Walter', 'lastName' => 'White', 'email' => '*****@*****.**', 'visibility' => 1);
     $ch = curl_init(TEST_BASE_URL . 'api2/Contacts');
     $options = array(CURLOPT_POSTFIELDS => json_encode($contact), CURLOPT_HTTPHEADER => array('Content-Type: application/json'), CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true);
     foreach ($this->authCurlOpts() as $opt => $optVal) {
         $options[$opt] = $optVal;
     }
     curl_setopt_array($ch, $options);
     $response = curl_exec($ch);
     $c = Contacts::model()->findByAttributes($contact);
     $this->assertResponseCodeIs(201, $ch);
     $this->assertNotEmpty($c);
     // 3. Test that the receiving end got the payload
     $outputDir = implode(DIRECTORY_SEPARATOR, array(Yii::app()->basePath, 'tests', 'data', 'output'));
     $this->assertFileExists($outputDir . DIRECTORY_SEPARATOR . "hook_{$hookName}.json");
     $contactPulled = json_decode(file_get_contents($outputDir . DIRECTORY_SEPARATOR . "hook_pulled_{$hookName}.json"), 1);
     foreach ($contact as $field => $value) {
         $this->assertEquals($value, $contactPulled[$field]);
     }
 }
Пример #2
0
 /**
  * Action for the creation of "hooks" (see {@link ApiHook})
  *
  * @param integer $_id ID of the hook.
  * @param string $_class Subclass of {@link X2Model} to which the hook pertains
  */
 public function actionHooks($_id = null, $_class = null)
 {
     $method = Yii::app()->request->getRequestType();
     if ($method == 'DELETE') {
         $hook = ApiHook::model()->findByPk($_id);
         if (!$hook instanceof ApiHook) {
             $this->send(404, '"Hook not found." -Smee');
         } elseif (!$hook->userId != Yii::app()->getSuId()) {
             $this->send(403, 'You cannot delete other API users\' hooks in X2Engine.');
         }
         $hook->setScenario('delete.remote');
         if ($hook->delete()) {
             $this->sendEmpty("Successfully unsubscribed from hook with " . "return URL {$hook->target_url}.");
         }
     } else {
         // POST (will respond to all other methods with 405)
         if ($_id !== null) {
             $this->send(405, 'Cannot manipulate preexisting hooks with POST.');
         }
         $hook = new ApiHook();
         $hook->attributes = $this->getJpost();
         $hook->userId = Yii::app()->getSuId();
         if (!empty($_class)) {
             $hook->modelName = get_class($this->staticModel);
         }
         if (!$hook->validate('event')) {
             $this->send(429, "The maximum number of hooks ({$maximum}) has " . "been reached for events of this type.");
         }
         if (!$hook->validate()) {
             $this->response['errors'] = $hook->errors;
             $this->send(422);
         }
         if ($hook->save()) {
             $this->response->httpHeader['Location'] = $this->createAbsoluteUrl('/api2/hooks', array('_id' => $hook->id));
             $this->responseBody = $hook;
             $this->send(201);
         } else {
             $this->send(500, 'Could not save hook due to unexpected ' . 'internal server error.');
         }
     }
 }