Merge pull request #14734 from yashodha/dev-1104
[civicrm-core.git] / tests / phpunit / api / v3 / MailSettingsTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class api_v3_MailSettingsTest
30 *
31 * @group headless
32 */
33 class api_v3_MailSettingsTest extends CiviUnitTestCase {
34
35 protected $_apiversion = 3;
36
37 protected $params;
38
39 protected $id;
40
41 public $DBResetRequired = FALSE;
42
43 public function setUp() {
44 $this->params = [
45 'domain_id' => 1,
46 'name' => "my mail setting",
47 'domain' => 'setting.com',
48 'localpart' => 'civicrm+',
49 'server' => "localhost",
50 'username' => 'sue',
51 'password' => 'pass',
52 'is_default' => 1,
53 ];
54 parent::setUp();
55 $this->useTransaction(TRUE);
56 }
57
58 /**
59 * Test creation.
60 *
61 * @param int $version
62 *
63 * @dataProvider versionThreeAndFour
64 */
65 public function testCreateMailSettings($version) {
66 $this->_apiversion = $version;
67 $this->callAPISuccessGetCount('mail_settings', [], 1);
68 $result = $this->callAPIAndDocument('MailSettings', 'create', $this->params, __FUNCTION__, __FILE__);
69 $this->assertEquals(1, $result['count']);
70 $this->assertNotNull($result['values'][$result['id']]['id']);
71 $this->callAPISuccess('MailSettings', 'delete', ['id' => $result['id']]);
72 $this->callAPISuccessGetCount('mail_settings', [], 1);
73 }
74
75 /**
76 * Test caches cleared adequately.
77 *
78 * @param int $version
79 *
80 * @dataProvider versionThreeAndFour
81 */
82 public function testCreateUpdateMailSettings($version) {
83 $this->_apiversion = $version;
84 $result = $this->callAPISuccess('MailSettings', 'create', $this->params);
85 $this->assertEquals('setting.com', CRM_Core_BAO_MailSettings::defaultDomain());
86 $this->callAPISuccess('mail_settings', 'create', ['id' => $result['id'], 'domain' => 'updated.com']);
87 $this->assertEquals('updated.com', CRM_Core_BAO_MailSettings::defaultDomain());
88 $this->callAPISuccess('MailSettings', 'delete', ['id' => $result['id']]);
89 $this->callAPISuccessGetCount('mail_settings', [], 1);
90 }
91
92 /**
93 * Test get method.
94 *
95 * @param int $version
96 *
97 * @dataProvider versionThreeAndFour
98 */
99 public function testGetMailSettings($version) {
100 $this->_apiversion = $version;
101 $this->callAPIAndDocument('MailSettings', 'create', $this->params, __FUNCTION__, __FILE__);
102 $result = $this->callAPIAndDocument('MailSettings', 'get', $this->params, __FUNCTION__, __FILE__);
103 $this->assertEquals(1, $result['count']);
104 $this->assertNotNull($result['values'][$result['id']]['id']);
105 $this->callAPISuccess('MailSettings', 'delete', ['id' => $result['id']]);
106 $this->callAPISuccessGetCount('mail_settings', [], 1);
107 }
108
109 /**
110 * @param int $version
111 *
112 * @dataProvider versionThreeAndFour
113 */
114 public function testDeleteMailSettings($version) {
115 $this->_apiversion = $version;
116 $this->callAPIAndDocument('MailSettings', 'create', $this->params, __FUNCTION__, __FILE__);
117 $entity = $this->callAPISuccess('MailSettings', 'get', $this->params);
118 $this->assertEquals('setting.com', $entity['values'][$entity['id']]['domain']);
119 $this->callAPIAndDocument('MailSettings', 'delete', ['id' => $entity['id']], __FUNCTION__, __FILE__);
120 $checkDeleted = $this->callAPISuccess('MailSettings', 'get', []);
121 $this->assertEquals('EXAMPLE.ORG', $checkDeleted['values'][$checkDeleted['id']]['domain']);
122 }
123
124 /**
125 * Test chained delete.
126 *
127 * @param int $version
128 *
129 * @dataProvider versionThreeAndFour
130 */
131 public function testGetMailSettingsChainDelete($version) {
132 $this->_apiversion = $version;
133 $description = "Demonstrates get + delete in the same call.";
134 $subFile = 'ChainedGetDelete';
135 $params = [
136 'name' => "delete this setting",
137 'api.MailSettings.delete' => 1,
138 ];
139 $this->callAPISuccess('MailSettings', 'create', ['name' => "delete this setting"] + $this->params);
140 $result = $this->callAPIAndDocument('MailSettings', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
141 $this->assertEquals(0, $this->callAPISuccess('MailSettings', 'getcount', ['name' => "delete this setting"]));
142 }
143
144 }