Merge pull request #14103 from jitendrapurohit/core-889
[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 * @group headless
31 */
32 class api_v3_MailSettingsTest extends CiviUnitTestCase {
33 protected $_apiversion = 3;
34 protected $params;
35 protected $id;
36 public $DBResetRequired = FALSE;
37
38 public function setUp() {
39 $this->params = array(
40 'domain_id' => 1,
41 'name' => "my mail setting",
42 'domain' => 'setting.com',
43 'localpart' => 'civicrm+',
44 'server' => "localhost",
45 'username' => 'sue',
46 'password' => 'pass',
47 'is_default' => 1,
48 );
49 parent::setUp();
50 $this->useTransaction(TRUE);
51 }
52
53 /**
54 * Test creation.
55 * @param int $version
56 * @dataProvider versionThreeAndFour
57 */
58 public function testCreateMailSettings($version) {
59 $this->_apiversion = $version;
60 $this->callAPISuccessGetCount('mail_settings', array(), 1);
61 $result = $this->callAPIAndDocument('MailSettings', 'create', $this->params, __FUNCTION__, __FILE__);
62 $this->assertEquals(1, $result['count']);
63 $this->assertNotNull($result['values'][$result['id']]['id']);
64 $this->callAPISuccess('MailSettings', 'delete', array('id' => $result['id']));
65 $this->callAPISuccessGetCount('mail_settings', array(), 1);
66 }
67
68 /**
69 * Test caches cleared adequately.
70 * @param int $version
71 * @dataProvider versionThreeAndFour
72 */
73 public function testCreateUpdateMailSettings($version) {
74 $this->_apiversion = $version;
75 $result = $this->callAPISuccess('MailSettings', 'create', $this->params);
76 $this->assertEquals('setting.com', CRM_Core_BAO_MailSettings::defaultDomain());
77 $this->callAPISuccess('mail_settings', 'create', array('id' => $result['id'], 'domain' => 'updated.com'));
78 $this->assertEquals('updated.com', CRM_Core_BAO_MailSettings::defaultDomain());
79 $this->callAPISuccess('MailSettings', 'delete', array('id' => $result['id']));
80 $this->callAPISuccessGetCount('mail_settings', array(), 1);
81 }
82
83 /**
84 * Test get method.
85 * @param int $version
86 * @dataProvider versionThreeAndFour
87 */
88 public function testGetMailSettings($version) {
89 $this->_apiversion = $version;
90 $this->callAPIAndDocument('MailSettings', 'create', $this->params, __FUNCTION__, __FILE__);
91 $result = $this->callAPIAndDocument('MailSettings', 'get', $this->params, __FUNCTION__, __FILE__);
92 $this->assertEquals(1, $result['count']);
93 $this->assertNotNull($result['values'][$result['id']]['id']);
94 $this->callAPISuccess('MailSettings', 'delete', array('id' => $result['id']));
95 $this->callAPISuccessGetCount('mail_settings', array(), 1);
96 }
97
98 /**
99 * @param int $version
100 * @dataProvider versionThreeAndFour
101 */
102 public function testDeleteMailSettings($version) {
103 $this->_apiversion = $version;
104 $this->callAPIAndDocument('MailSettings', 'create', $this->params, __FUNCTION__, __FILE__);
105 $entity = $this->callAPISuccess('MailSettings', 'get', $this->params);
106 $this->assertEquals('setting.com', $entity['values'][$entity['id']]['domain']);
107 $this->callAPIAndDocument('MailSettings', 'delete', array('id' => $entity['id']), __FUNCTION__, __FILE__);
108 $checkDeleted = $this->callAPISuccess('MailSettings', 'get', array());
109 $this->assertEquals('EXAMPLE.ORG', $checkDeleted['values'][$checkDeleted['id']]['domain']);
110 }
111
112 /**
113 * Test chained delete.
114 * @param int $version
115 * @dataProvider versionThreeAndFour
116 */
117 public function testGetMailSettingsChainDelete($version) {
118 $this->_apiversion = $version;
119 $description = "Demonstrates get + delete in the same call.";
120 $subFile = 'ChainedGetDelete';
121 $params = array(
122 'name' => "delete this setting",
123 'api.MailSettings.delete' => 1,
124 );
125 $this->callAPISuccess('MailSettings', 'create', ['name' => "delete this setting"] + $this->params);
126 $result = $this->callAPIAndDocument('MailSettings', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
127 $this->assertEquals(0, $this->callAPISuccess('MailSettings', 'getcount', ['name' => "delete this setting"]));
128 }
129
130 }