All Data Structures Functions Variables Pages
ApiRestEmailMessageTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
42  public static function setUpBeforeClass()
43  {
44  parent::setUpBeforeClass();
45  $super = User::getByUsername('super');
46  $super->primaryEmail->emailAddress = 'senderTest@example.com';
47  $super->save();
48  }
49 
50  public function testCreateEmailMessage()
51  {
52  $super = User::getByUsername('super');
53  Yii::app()->user->userModel = $super;
54  $authenticationData = $this->login();
55  $headers = array(
56  'Accept: application/json',
57  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
58  'ZURMO_TOKEN: ' . $authenticationData['token'],
59  'ZURMO_API_REQUEST_TYPE: REST',
60  );
61 
62  // Test with at least one existing recipient
63  $data['subject'] = 'Test 1 Subject';
64  $data['textContent'] = 'Test 1 Text Content';
65  $data['htmlContent'] = 'Test 1 Html Content';
66  $data['sentFrom']['email'] = 'senderTest@example.com';
67  $data['recipients'] = array(
68  'to' => array(
69  array('name' => 'TO1', 'email' => 'to1@example.com'),
70  array('name' => 'TO2', 'email' => 'to2@example.com')
71  ),
72  'cc' => array(
73  array('name' => 'CC1', 'email' => 'cc1@example.com'),
74  array('name' => 'CC2', 'email' => 'cc2@example.com')
75  )
76  );
77 
78  $contact1 = ContactTestHelper::createContactByNameForOwner('TestContact1', $super);
79  $contact1->primaryEmail->emailAddress = 'to1@example.com';
80  $contact1->save();
81 
82  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
83  $response = json_decode($response, true);
84  $this->assertArrayHasKey('id', $response['data']);
85  $emailMessageId = $response['data']['id'];
86  $emailMessage = EmailMessage::getById($emailMessageId);
87 
88  $this->assertEquals('Test 1 Subject', $emailMessage->subject);
89  $this->assertEquals('Test 1 Text Content', $emailMessage->content->textContent);
90  $this->assertEquals('Test 1 Html Content', $emailMessage->content->htmlContent);
91  $this->assertEquals('senderTest@example.com', $emailMessage->sender->fromAddress);
92  $this->assertEquals(EmailFolder::TYPE_ARCHIVED, $emailMessage->folder->type);
93  $this->assertEquals(4, count($emailMessage->recipients));
94  $this->assertEquals($data['recipients']['to'][0]['email'], $emailMessage->recipients[0]->toAddress);
95  $this->assertEquals($data['recipients']['to'][0]['name'], $emailMessage->recipients[0]->toName);
96  $this->assertEquals(EmailMessageRecipient::TYPE_TO, $emailMessage->recipients[0]->type);
97  $this->assertEquals($data['recipients']['to'][1]['email'], $emailMessage->recipients[1]->toAddress);
98  $this->assertEquals($data['recipients']['to'][1]['name'], $emailMessage->recipients[1]->toName);
99  $this->assertEquals(EmailMessageRecipient::TYPE_TO, $emailMessage->recipients[1]->type);
100  $this->assertEquals($data['recipients']['cc'][0]['email'], $emailMessage->recipients[2]->toAddress);
101  $this->assertEquals($data['recipients']['cc'][0]['name'], $emailMessage->recipients[2]->toName);
102  $this->assertEquals(EmailMessageRecipient::TYPE_CC, $emailMessage->recipients[2]->type);
103  $this->assertEquals($data['recipients']['cc'][1]['email'], $emailMessage->recipients[3]->toAddress);
104  $this->assertEquals($data['recipients']['cc'][1]['name'], $emailMessage->recipients[3]->toName);
105  $this->assertEquals(EmailMessageRecipient::TYPE_CC, $emailMessage->recipients[3]->type);
106 
107  // Test without existing recipient
108  $data['subject'] = 'Test 2 Subject';
109  $data['textContent'] = 'Test 2 Text Content';
110  $data['htmlContent'] = 'Test 2 Html Content';
111  $data['sentFrom']['email'] = 'senderTest@example.com';
112  $data['recipients'] = array(
113  'to' => array(
114  array('name' => 'TO11', 'email' => 'to11@example.com'),
115  array('name' => 'TO21', 'email' => 'to21@example.com')
116  ),
117  );
118 
119  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
120  $response = json_decode($response, true);
121  $this->assertArrayHasKey('id', $response['data']);
122  $emailMessageId = $response['data']['id'];
123  $emailMessage = EmailMessage::getById($emailMessageId);
124 
125  $this->assertEquals('Test 2 Subject', $emailMessage->subject);
126  $this->assertEquals('Test 2 Text Content', $emailMessage->content->textContent);
127  $this->assertEquals('Test 2 Html Content', $emailMessage->content->htmlContent);
128  $this->assertEquals('senderTest@example.com', $emailMessage->sender->fromAddress);
129  $this->assertEquals(EmailFolder::TYPE_ARCHIVED_UNMATCHED, $emailMessage->folder->type);
130  $this->assertEquals(2, count($emailMessage->recipients));
131  }
132 
133  public function testCreateEmailMessageWithoutRecipients()
134  {
135  $super = User::getByUsername('super');
136  Yii::app()->user->userModel = $super;
137  $authenticationData = $this->login();
138  $headers = array(
139  'Accept: application/json',
140  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
141  'ZURMO_TOKEN: ' . $authenticationData['token'],
142  'ZURMO_API_REQUEST_TYPE: REST',
143  );
144 
145  $data['subject'] = 'Test 1 Subject';
146  $data['textContent'] = 'Test 1 Text Content';
147  $data['htmlContent'] = 'Test 1 Html Content';
148  $data['sentFrom']['email'] = 'senderTest@example.com';
149 
150  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
151  $response = json_decode($response, true);
152  $this->assertEquals('No recipients found.', $response['message']);
153  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
154  }
155 
156  public function testCreateEmailMessageWithoutSender()
157  {
158  $super = User::getByUsername('super');
159  Yii::app()->user->userModel = $super;
160  $authenticationData = $this->login();
161  $headers = array(
162  'Accept: application/json',
163  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
164  'ZURMO_TOKEN: ' . $authenticationData['token'],
165  'ZURMO_API_REQUEST_TYPE: REST',
166  );
167 
168  $data['subject'] = 'Test 1 Subject';
169  $data['textContent'] = 'Test 1 Text Content';
170  $data['htmlContent'] = 'Test 1 Html Content';
171  $data['recipients'] = array(
172  'to' => array(
173  array('name' => 'TO1', 'email' => 'to1@example.com'),
174  array('name' => 'TO2', 'email' => 'to2@example.com')
175  ),
176  );
177 
178  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
179  $response = json_decode($response, true);
180  $this->assertEquals('Sender not found.', $response['message']);
181  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
182  }
183 
184  public function testCreateEmailMessageWithoutBody()
185  {
186  $super = User::getByUsername('super');
187  Yii::app()->user->userModel = $super;
188  $authenticationData = $this->login();
189  $headers = array(
190  'Accept: application/json',
191  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
192  'ZURMO_TOKEN: ' . $authenticationData['token'],
193  'ZURMO_API_REQUEST_TYPE: REST',
194  );
195 
196  $data['subject'] = 'Test 1 Subject';
197  $data['sentFrom']['email'] = 'senderTest@example.com';
198  $data['recipients'] = array(
199  'to' => array(
200  array('name' => 'TO1', 'email' => 'to1@example.com'),
201  array('name' => 'TO2', 'email' => 'to2@example.com')
202  ),
203  );
204 
205  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
206  $response = json_decode($response, true);
207  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
208  $this->assertEquals('No email content found.', $response['message']);
209  }
210 
211  public function testCreateEmailMessageWithSpecificOwner()
212  {
213  $super = User::getByUsername('super');
214  Yii::app()->user->userModel = $super;
215  $billy = User::getByUsername('billy');
216 
217  $authenticationData = $this->login();
218  $headers = array(
219  'Accept: application/json',
220  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
221  'ZURMO_TOKEN: ' . $authenticationData['token'],
222  'ZURMO_API_REQUEST_TYPE: REST',
223  );
224 
225  $data['subject'] = 'Test 1 Subject';
226  $data['textContent'] = 'Test 1 Text Content';
227  $data['htmlContent'] = 'Test 1 Html Content';
228  $data['sentFrom']['email'] = 'senderTest@example.com';
229  $data['recipients'] = array(
230  'to' => array(
231  array('name' => 'TO1', 'email' => 'to1@example.com'),
232  array('name' => 'TO2', 'email' => 'to2@example.com')
233  ),
234  );
235  $data['owner']['id'] = $billy->id;
236 
237  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
238  $response = json_decode($response, true);
239  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
240  $this->assertArrayHasKey('id', $response['data']);
241  $emailMessageId = $response['data']['id'];
242  $emailMessage = EmailMessage::getById($emailMessageId);
243 
244  $this->assertEquals('Test 1 Subject', $emailMessage->subject);
245  $this->assertEquals('Test 1 Text Content', $emailMessage->content->textContent);
246  $this->assertEquals('Test 1 Html Content', $emailMessage->content->htmlContent);
247  $this->assertEquals('senderTest@example.com', $emailMessage->sender->fromAddress);
248  $this->assertEquals($billy->id, $emailMessage->owner->id);
249  $this->assertEquals(2, count($emailMessage->recipients));
250  }
251 
252  public function testCreateEmailMessageWithSpecificSentDateTime()
253  {
254  $super = User::getByUsername('super');
255  Yii::app()->user->userModel = $super;
256 
257  $authenticationData = $this->login();
258  $headers = array(
259  'Accept: application/json',
260  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
261  'ZURMO_TOKEN: ' . $authenticationData['token'],
262  'ZURMO_API_REQUEST_TYPE: REST',
263  );
264 
265  $data['subject'] = 'Test 1 Subject';
266  $data['textContent'] = 'Test 1 Text Content';
267  $data['htmlContent'] = 'Test 1 Html Content';
268  $data['sentDateTime'] = '2015-01-01 00:00:01';
269  $data['sentFrom']['email'] = 'senderTest@example.com';
270  $data['recipients'] = array(
271  'to' => array(
272  array('name' => 'TO1', 'email' => 'to1@example.com'),
273  array('name' => 'TO2', 'email' => 'to2@example.com')
274  ),
275  );
276 
277  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
278  $response = json_decode($response, true);
279  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
280  $this->assertArrayHasKey('id', $response['data']);
281  $emailMessageId = $response['data']['id'];
282  $emailMessage = EmailMessage::getById($emailMessageId);
283 
284  $this->assertEquals('Test 1 Subject', $emailMessage->subject);
285  $this->assertEquals('Test 1 Text Content', $emailMessage->content->textContent);
286  $this->assertEquals('Test 1 Html Content', $emailMessage->content->htmlContent);
287  $this->assertEquals('2015-01-01 00:00:01', $emailMessage->sentDateTime);
288  $this->assertEquals('senderTest@example.com', $emailMessage->sender->fromAddress);
289 
290  //Test with invalid sentDateTime
291  $data['subject'] = 'Test 1 Subject';
292  $data['textContent'] = 'Test 1 Text Content';
293  $data['htmlContent'] = 'Test 1 Html Content';
294  $data['sentDateTime'] = '2015-01-01 00:0';//invalid DateTime
295  $data['sentFrom']['email'] = 'senderTest@example.com';
296  $data['recipients'] = array(
297  'to' => array(
298  array('name' => 'TO1', 'email' => 'to1@example.com'),
299  array('name' => 'TO2', 'email' => 'to2@example.com')
300  ),
301  );
302 
303  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
304  $response = json_decode($response, true);
305  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
306  $this->assertEquals('Model was not created.', $response['message']);
307  $this->assertArrayHasKey('sentDateTime', $response['errors']);
308  $this->assertEquals('Sent Date Time must be datetime.', $response['errors']['sentDateTime'][0]);
309  }
310 
311  public function testCreateEmailMessageWithBinaryAttachments()
312  {
313  $super = User::getByUsername('super');
314  Yii::app()->user->userModel = $super;
315 
316  $authenticationData = $this->login();
317  $headers = array(
318  'Accept: application/json',
319  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
320  'ZURMO_TOKEN: ' . $authenticationData['token'],
321  'ZURMO_API_REQUEST_TYPE: REST',
322  );
323 
324  $data['subject'] = 'Test 1 Subject';
325  $data['textContent'] = 'Test 1 Text Content';
326  $data['htmlContent'] = 'Test 1 Html Content';
327  $data['sentFrom']['email'] = 'senderTest@example.com';
328  $data['recipients'] = array(
329  'to' => array(
330  array('name' => 'TO1', 'email' => 'to1@example.com'),
331  array('name' => 'TO2', 'email' => 'to2@example.com')
332  ),
333  );
334  $pathToFiles = Yii::getPathOfAlias('application.modules.api.tests.unit.files');
335  $filePath_1 = $pathToFiles . DIRECTORY_SEPARATOR . 'table.csv';
336  $filePath_2 = $pathToFiles . DIRECTORY_SEPARATOR . 'image.png';
337  $filePath_3 = $pathToFiles . DIRECTORY_SEPARATOR . 'text.txt';
338  $filePath_4 = $pathToFiles . DIRECTORY_SEPARATOR . 'text.abc';
339  $data['attachments'] = array(
340  array('filename' => 'table.csv', 'attachment' => base64_encode(file_get_contents($filePath_1))),
341  array('filename' => 'image.png', 'attachment' => base64_encode(file_get_contents($filePath_2))),
342  array('filename' => 'text.txt', 'attachment' => base64_encode(file_get_contents($filePath_3))),
343  array('filename' => 'text.abc', 'attachment' => base64_encode(file_get_contents($filePath_4))), //extension not allowed
344  array('name' => 'text.abc', 'file' => base64_encode(file_get_contents($filePath_4))), //invalid data format
345  );
346 
347  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
348  $response = json_decode($response, true);
349  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
350  $this->assertArrayHasKey('id', $response['data']);
351  $emailMessageId = $response['data']['id'];
352  $emailMessage = EmailMessage::getById($emailMessageId);
353 
354  $this->assertEquals('Test 1 Subject', $emailMessage->subject);
355  $this->assertEquals('Test 1 Text Content', $emailMessage->content->textContent);
356  $this->assertEquals('Test 1 Html Content', $emailMessage->content->htmlContent);
357  $this->assertEquals('senderTest@example.com', $emailMessage->sender->fromAddress);
358  $this->assertEquals(3, count($emailMessage->files));
359  $this->assertEquals($data['attachments'][0]['filename'], $emailMessage->files[0]->name);
360  $this->assertEquals(filesize($filePath_1), $emailMessage->files[0]->size);
361  $this->assertEquals(md5_file($filePath_1), md5($emailMessage->files[0]->fileContent->content));
362  $this->assertEquals($data['attachments'][1]['filename'], $emailMessage->files[1]->name);
363  $this->assertEquals(filesize($filePath_2), $emailMessage->files[1]->size);
364  $this->assertEquals(md5_file($filePath_2), md5($emailMessage->files[1]->fileContent->content));
365  $this->assertEquals($data['attachments'][2]['filename'], $emailMessage->files[2]->name);
366  $this->assertEquals(filesize($filePath_3), $emailMessage->files[2]->size);
367  $this->assertEquals(md5_file($filePath_3), md5($emailMessage->files[2]->fileContent->content));
368  }
369 
370  protected function getApiControllerClassName()
371  {
372  Yii::import('application.modules.emailMessages.controllers.EmailMessageApiController', true);
373  return 'EmailMessagesEmailMessageApiController';
374  }
375 
376  protected function getModuleBaseApiUrl()
377  {
378  return 'emailMessages/emailMessage/api/';
379  }
380  }
381 ?>
static getByUsername($username)
Definition: User.php:49
static getById($id, $modelClassName=null)
Generated on Sat Feb 22 2020 07:10:28