function setUp()
 {
     parent::setUp();
     $this->modules = array('one' => new CRM_Core_Module('com.example.one', TRUE), 'two' => new CRM_Core_Module('com.example.two', TRUE));
     $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_managed');
     $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name like "CRM_Example_%"');
     $this->fixtures['com.example.one-foo'] = array('module' => 'com.example.one', 'name' => 'foo', 'entity' => 'CustomSearch', 'params' => array('version' => 3, 'class_name' => 'CRM_Example_One_Foo', 'is_reserved' => 1));
     $this->fixtures['com.example.one-bar'] = array('module' => 'com.example.one', 'name' => 'bar', 'entity' => 'CustomSearch', 'params' => array('version' => 3, 'class_name' => 'CRM_Example_One_Bar', 'is_reserved' => 1));
     $this->apiKernel = \Civi\Core\Container::singleton()->get('civi_api_kernel');
     $this->adhocProvider = new \Civi\API\Provider\AdhocProvider(3, 'CustomSearch');
     $this->apiKernel->registerApiProvider($this->adhocProvider);
 }
Ejemplo n.º 2
0
 public function setUp()
 {
     $this->useTransaction(TRUE);
     parent::setUp();
     $this->modules = array('one' => new CRM_Core_Module('com.example.one', TRUE), 'two' => new CRM_Core_Module('com.example.two', TRUE));
     // Testing on drupal-demo fails because some extensions have mgd ents.
     CRM_Core_DAO::singleValueQuery('DELETE FROM civicrm_managed');
     $this->fixtures['com.example.one-foo'] = array('module' => 'com.example.one', 'name' => 'foo', 'entity' => 'CustomSearch', 'params' => array('version' => 3, 'class_name' => 'CRM_Example_One_Foo', 'is_reserved' => 1));
     $this->fixtures['com.example.one-bar'] = array('module' => 'com.example.one', 'name' => 'bar', 'entity' => 'CustomSearch', 'params' => array('version' => 3, 'class_name' => 'CRM_Example_One_Bar', 'is_reserved' => 1));
     $this->apiKernel = \Civi::service('civi_api_kernel');
     $this->adhocProvider = new \Civi\API\Provider\AdhocProvider(3, 'CustomSearch');
     $this->apiKernel->registerApiProvider($this->adhocProvider);
 }
Ejemplo n.º 3
0
 public function testResolveException()
 {
     $test = $this;
     $this->dispatcher->addListener(Events::RESOLVE, function () {
         throw new \API_Exception('Oh My God', 'omg', array('the' => 'badzes'));
     }, Events::W_EARLY);
     $this->dispatcher->addListener(Events::EXCEPTION, function (\Civi\API\Event\ExceptionEvent $event) use($test) {
         $test->assertEquals('Oh My God', $event->getException()->getMessage());
     });
     $this->kernel->registerApiProvider($this->createWidgetFrobnicateProvider());
     $result = $this->kernel->run('Widget', 'frobnicate', array('version' => self::MOCK_VERSION));
     $expectedEventSequence = array(array('name' => Events::RESOLVE, 'class' => 'Civi\\API\\Event\\ResolveEvent'), array('name' => Events::EXCEPTION, 'class' => 'Civi\\API\\Event\\ExceptionEvent'));
     $this->assertEquals($expectedEventSequence, $this->actualEventSequence);
     $this->assertEquals('Oh My God', $result['error_message']);
     $this->assertEquals('omg', $result['error_code']);
     $this->assertEquals('badzes', $result['the']);
 }
 /**
  * @param $entity
  * @param $action
  * @param array $params
  * @param $expectedError
  * @dataProvider badDataProvider
  */
 public function testBad($entity, $action, $params, $expectedError)
 {
     $params['version'] = 3;
     $params['debug'] = 1;
     $params['check_permissions'] = 1;
     $result = $this->kernel->run($entity, $action, $params);
     $this->assertTrue((bool) $result['is_error'], print_r(array('$entity' => $entity, '$action' => $action, '$params' => $params, '$result' => $result), TRUE));
     $this->assertRegExp($expectedError, $result['error_message']);
 }
 /**
  * {inheritdoc}
  */
 public function invoke($apiRequest)
 {
     if ($apiRequest['entity'] == 'Entity' && $apiRequest['action'] == 'get') {
         return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']));
     }
     switch ($apiRequest['action']) {
         case 'getactions':
             return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']));
             //      case 'getfields':
             //        return $this->doGetFields($apiRequest);
         //      case 'getfields':
         //        return $this->doGetFields($apiRequest);
         default:
     }
     // We shouldn't get here because onApiResolve() checks $this->actions
     throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
 }
 /**
  * @param array $apiRequest
  *   Array(entity=>$,action=>$,params=>$,expectedResults=>$).
  * @param array $rules
  *   Whitelist - list of allowed API calls/patterns.
  * @param bool $expectSuccess
  *   TRUE if the call should succeed.
  *   Success implies that the 'expectedResults' are returned.
  *   Failure implies that the standard error message is returned.
  * @dataProvider restrictionCases
  */
 public function testEach($apiRequest, $rules, $expectSuccess)
 {
     \CRM_Core_DAO_AllCoreTables::init(TRUE);
     $recs = $this->getFixtures();
     \CRM_Core_DAO_AllCoreTables::registerEntityType('Widget', 'CRM_Fake_DAO_Widget', 'fake_widget');
     $widgetProvider = new \Civi\API\Provider\StaticProvider(3, 'Widget', array('id', 'widget_type', 'provider', 'title'), array(), $recs['widget']);
     \CRM_Core_DAO_AllCoreTables::registerEntityType('Sprocket', 'CRM_Fake_DAO_Sprocket', 'fake_sprocket');
     $sprocketProvider = new \Civi\API\Provider\StaticProvider(3, 'Sprocket', array('id', 'sprocket_type', 'widget_id', 'provider', 'title', 'comment'), array(), $recs['sprocket']);
     $whitelist = WhitelistRule::createAll($rules);
     $dispatcher = new EventDispatcher();
     $kernel = new Kernel($dispatcher);
     $kernel->registerApiProvider($sprocketProvider);
     $kernel->registerApiProvider($widgetProvider);
     $dispatcher->addSubscriber(new WhitelistSubscriber($whitelist));
     $dispatcher->addSubscriber(new ChainSubscriber());
     $apiRequest['params']['debug'] = 1;
     $apiRequest['params']['check_permissions'] = 'whitelist';
     $result = $kernel->run($apiRequest['entity'], $apiRequest['action'], $apiRequest['params']);
     if ($expectSuccess) {
         $this->assertAPISuccess($result);
         $this->assertTrue(is_array($apiRequest['expectedResults']));
         $this->assertTreeEquals($apiRequest['expectedResults'], $result['values']);
     } else {
         $this->assertAPIFailure($result);
         $this->assertRegExp('/The request does not match any active API authorizations./', $result['error_message']);
     }
 }
Ejemplo n.º 7
0
 /**
  * Call any nested api calls.
  *
  * TODO: We don't really need this to be a separate function.
  * @param \Civi\API\Kernel $apiKernel
  * @param $params
  * @param $result
  * @param $action
  * @param $entity
  * @param $version
  * @throws \Exception
  */
 protected function callNestedApi($apiKernel, &$params, &$result, $action, $entity, $version)
 {
     $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity);
     // We don't need to worry about nested api in the getfields/getoptions
     // actions, so just return immediately.
     if (in_array($action, array('getfields', 'getfield', 'getoptions'))) {
         return;
     }
     if ($action == 'getsingle') {
         // I don't understand the protocol here, but we don't want
         // $result to be a recursive array
         // $result['values'][0] = $result;
         $oldResult = $result;
         $result = array('values' => array(0 => $oldResult));
     }
     foreach ($params as $field => $newparams) {
         if ((is_array($newparams) || $newparams === 1) && $field != 'api.has_parent' && substr($field, 0, 3) == 'api') {
             // 'api.participant.delete' => 1 is a valid options - handle 1
             // instead of an array
             if ($newparams === 1) {
                 $newparams = array('version' => $version);
             }
             // can be api_ or api.
             $separator = $field[3];
             if (!($separator == '.' || $separator == '_')) {
                 continue;
             }
             $subAPI = explode($separator, $field);
             $subaction = empty($subAPI[2]) ? $action : $subAPI[2];
             $subParams = array('debug' => \CRM_Utils_Array::value('debug', $params));
             $subEntity = _civicrm_api_get_entity_name_from_camel($subAPI[1]);
             foreach ($result['values'] as $idIndex => $parentAPIValues) {
                 if ($subEntity != 'contact') {
                     //contact spits the dummy at activity_id so what else won't it like?
                     //set entity_id & entity table based on the parent's id & entity.
                     //e.g for something like note if the parent call is contact
                     //'entity_table' will be set to 'contact' & 'id' to the contact id
                     //from the parent call. in this case 'contact_id' will also be
                     //set to the parent's id
                     if (!($subEntity == 'line_item' && $lowercase_entity == 'contribution' && $action != 'create')) {
                         $subParams["entity_id"] = $parentAPIValues['id'];
                         $subParams['entity_table'] = 'civicrm_' . $lowercase_entity;
                     }
                     $crm16084 = FALSE;
                     if ($subEntity == 'relationship' && $lowercase_entity == 'contact') {
                         // if a relationship call is chained to a contact call, we need
                         // to check whether contact_id_a or contact_id_b for the
                         // relationship is given. If so, don't add an extra subParam
                         // "contact_id" => parent_id.
                         // See CRM-16084.
                         foreach (array_keys($newparams) as $key) {
                             if (substr($key, 0, 11) == 'contact_id_') {
                                 $crm16084 = TRUE;
                                 break;
                             }
                         }
                     }
                     if (!$crm16084) {
                         $subParams[$lowercase_entity . "_id"] = $parentAPIValues['id'];
                     }
                 }
                 if ($entity != 'Contact' && \CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) {
                     //e.g. if event_id is in the values returned & subentity is event
                     //then pass in event_id as 'id' don't do this for contact as it
                     //does some weird things like returning primary email &
                     //thus limiting the ability to chain email
                     //TODO - this might need the camel treatment
                     $subParams['id'] = $parentAPIValues[$subEntity . "_id"];
                 }
                 if (\CRM_Utils_Array::value('entity_table', $result['values'][$idIndex]) == $subEntity) {
                     $subParams['id'] = $result['values'][$idIndex]['entity_id'];
                 }
                 // if we are dealing with the same entity pass 'id' through
                 // (useful for get + delete for example)
                 if ($lowercase_entity == $subEntity) {
                     $subParams['id'] = $result['values'][$idIndex]['id'];
                 }
                 $subParams['version'] = $version;
                 if (!empty($params['check_permissions'])) {
                     $subParams['check_permissions'] = $params['check_permissions'];
                 }
                 $subParams['sequential'] = 1;
                 $subParams['api.has_parent'] = 1;
                 if (array_key_exists(0, $newparams)) {
                     $genericParams = $subParams;
                     // it is a numerically indexed array - ie. multiple creates
                     foreach ($newparams as $entityparams) {
                         $subParams = array_merge($genericParams, $entityparams);
                         _civicrm_api_replace_variables($subParams, $result['values'][$idIndex], $separator);
                         $result['values'][$idIndex][$field][] = $apiKernel->run($subEntity, $subaction, $subParams);
                         if ($result['is_error'] === 1) {
                             throw new \Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']);
                         }
                     }
                 } else {
                     $subParams = array_merge($subParams, $newparams);
                     _civicrm_api_replace_variables($subParams, $result['values'][$idIndex], $separator);
                     $result['values'][$idIndex][$field] = $apiKernel->run($subEntity, $subaction, $subParams);
                     if (!empty($result['is_error'])) {
                         throw new \Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']);
                     }
                 }
             }
         }
     }
     if ($action == 'getsingle') {
         $result = $result['values'][0];
     }
 }