(NFC) (dev/core#878) Simplify copyright header (tests/*)
[civicrm-core.git] / tests / phpunit / api / v3 / CustomApiTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for API functions.
14 *
15 * @package CiviCRM_APIv3
16 * @group headless
17 */
18 class api_v3_CustomApiTest extends CiviUnitTestCase {
19
20 protected $_apiversion = 3;
21
22 public function setUp() {
23 parent::setUp();
24 $this->installApi();
25 }
26
27 public function tearDown() {
28 parent::tearDown();
29 CRM_Core_DAO::executeQuery('DROP TABLE civicrm_mailing_provider_data');
30 }
31
32 /**
33 * Test that a custom api, as one would add in an extension works.
34 *
35 * This api is a bit 'special' in that it has a composite primary key rather
36 * than using 'id', make sure that works too....
37 */
38 public function testCustomApi() {
39 $this->installApi();
40 $this->callAPISuccess('MailingProviderData', 'create', [
41 'contact_identifier' => 'xyz',
42 'mailing_identifier' => 'abx',
43 ]);
44 $this->callAPISuccess('Mailing', 'create', ['name' => 'CiviMail', 'hash' => 'abx']);
45 $result = $this->callAPISuccess('MailingProviderData', 'get', ['return' => ['mailing_identifier.name', 'contact_identifier', 'mailing_identifier']]);
46 $this->assertEquals(1, $result['count']);
47 $this->assertEquals('xyzabx2017-01-01 00:00:00', $result['id']);
48 $this->assertEquals('xyzabx2017-01-01 00:00:00', $result['id']);
49 $this->assertEquals([
50 'contact_identifier' => 'xyz',
51 'mailing_identifier' => 'abx',
52 'mailing_identifier.name' => 'CiviMail',
53 ], reset($result['values']));
54 }
55
56 /**
57 * * Implements hook_civicrm_EntityTypes().
58 *
59 * @param array $entityTypes
60 */
61 public function hookEntityTypes(&$entityTypes) {
62 $entityTypes['CRM_Omnimail_DAO_MailingProviderData'] = [
63 'name' => 'MailingProviderData',
64 'class' => 'CRM_Omnimail_DAO_MailingProviderData',
65 'table' => 'civicrm_maiing_provider_data',
66 ];
67 }
68
69 /**
70 * Install the custom api.
71 */
72 public function installApi() {
73 require_once __DIR__ . '/custom_api/MailingProviderData.php';
74 $this->hookClass->setHook('civicrm_entityTypes', [$this, 'hookEntityTypes']);
75 CRM_Core_DAO_AllCoreTables::init(TRUE);
76 CRM_Core_DAO::executeQuery(
77 "CREATE TABLE IF NOT EXISTS `civicrm_mailing_provider_data` (
78 `contact_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
79 `mailing_identifier` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
80 `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
81 `event_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
82 `recipient_action_datetime` timestamp NOT NULL DEFAULT '2017-01-01 00:00:00',
83 `contact_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
84 `is_civicrm_updated` TINYINT(4) DEFAULT '0',
85 PRIMARY KEY (`contact_identifier`,`recipient_action_datetime`,`event_type`),
86 KEY `contact_identifier` (`contact_identifier`),
87 KEY `mailing_identifier` (`mailing_identifier`),
88 KEY `contact_id` (`contact_id`),
89 KEY `email` (`email`),
90 KEY `event_type` (`event_type`),
91 KEY `recipient_action_datetime` (`recipient_action_datetime`)
92 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
93 );
94 }
95
96 }