Merge branch 'master' of https://github.com/rollox/civicrm-core into CRM-18317
[civicrm-core.git] / tests / phpunit / WebTest / Campaign / MailingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
28
29 /**
30 * Class WebTest_Campaign_MailingTest
31 */
32 class WebTest_Campaign_MailingTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testCreateCampaign() {
39 // Log in as admin first to verify permissions for CiviCampaign
40 $this->webtestLogin('admin');
41
42 // Enable CiviCampaign module if necessary
43 $this->enableComponents(array('CiviMail', 'CiviCampaign'));
44
45 $this->setupDefaultMailbox();
46
47 // add the required permission
48 $this->changePermissions('edit-2-administer-civicampaign');
49
50 // Log in as normal user
51 $this->webtestLogin();
52
53 // Create new group
54 $title = substr(sha1(rand()), 0, 7);
55 $groupName = $this->WebtestAddGroup();
56
57 // Adding contact
58 // We're using Quick Add block on the main page for this.
59 $firstName = substr(sha1(rand()), 0, 7);
60 $this->webtestAddContact($firstName, "Smith", "$firstName.smith@example.org");
61 $this->_contactNames = array("$firstName.smith@example.org" => "Smith, $firstName");
62
63 // add contact to group
64 // visit group tab
65 $this->click("css=li#tab_group a");
66 $this->waitForElementPresent("group_id");
67
68 // add to group
69 $this->select("group_id", "label=$groupName");
70 $this->click("_qf_GroupContact_next");
71 $this->waitForElementPresent('link=Remove');
72
73 $this->openCiviPage('campaign/add', 'reset=1', '_qf_Campaign_upload-bottom');
74
75 $campaignTitle = "Campaign $title";
76 $this->type("title", $campaignTitle);
77
78 // select the campaign type
79 $this->select("campaign_type_id", "value=2");
80
81 // fill in the description
82 $this->type("description", "This is a test campaign");
83
84 // include groups for the campaign
85 $this->multiselect2("includeGroups", array("$groupName", "Advisory Board"));
86
87 // fill the end date for campaign
88 $this->webtestFillDate("end_date", "+1 year");
89
90 // select campaign status
91 $this->select("status_id", "value=2");
92
93 // click save
94 $this->click("_qf_Campaign_upload-bottom");
95 $this->waitForPageToLoad($this->getTimeoutMsec());
96
97 $this->waitForText('crm-notification-container', "Campaign $title");
98
99 $this->mailingAddTest($groupName, $campaignTitle, $title, $firstName);
100 }
101
102 /**
103 * Test mailing add.
104 *
105 * @param string $groupName
106 * @param string $campaignTitle
107 * @param string $title
108 * @param string $firstUserName
109 */
110 public function mailingAddTest($groupName, $campaignTitle, $title, $firstUserName) {
111 //---- create mailing contact and add to mailing Group
112 $firstName = substr(sha1(rand()), 0, 7);
113 $this->webtestAddContact($firstName, "Mailson", "mailino$firstName@mailson.co.in");
114 $this->_contactNames["mailino$firstName@mailson.co.in"] = "Mailson, $firstName";
115
116 // go to group tab and add to mailing group
117 $this->click("css=li#tab_group a");
118 $this->waitForElementPresent("_qf_GroupContact_next");
119 $this->select("group_id", "$groupName");
120 $this->click("_qf_GroupContact_next");
121
122 $this->openCiviPage('a/#/mailing/new');
123 $this->waitForElementPresent("xpath=//input[@name='mailingName']");
124 //-------select recipients----------
125
126 // fill mailing name
127 $mailingName = substr(sha1(rand()), 0, 7);
128 $this->type("xpath=//input[@name='mailingName']", "Mailing $mailingName Webtest");
129
130 // select campaign
131 $this->select2("s2id_crmUiId_4", "Campaign_" . $title);
132
133 // Add the test mailing group
134 $this->select2("s2id_crmUiId_8", $groupName, TRUE);
135
136 $this->waitForTextPresent("~2 recipients");
137
138 //--------Mailing content------------
139 $tokens = ' {domain.address}{action.optOutUrl}';
140 // fill subject for mailing
141 $this->type("xpath=//input[@name='subject']", "Test subject {$mailingName} for Webtest");
142 // HTML format message
143 $HTMLMessage = "This is HTML formatted content for Mailing {$mailingName} Webtest.";
144 $this->fillRichTextField("crmUiId_1", $HTMLMessage . $tokens);
145
146 // FIXME: Selenium can't access content in an iframe
147 //$this->click("xpath=//div[@class='preview-popup']//a[text()='Preview as HTML']");
148 //$this->waitForTextPresent($HTMLMessage);
149 //$this->waitForAjaxContent();
150 //$this->click("xpath=//button[@title='Close']");
151
152 // Open Plain-text Format pane and type text format msg
153 $this->click("//div[starts-with(text(),'Plain Text')]");
154 $this->type("xpath=//*[@name='body_text']", "This is text formatted content for Mailing {$mailingName} Webtest.$tokens");
155
156 $this->click("xpath=//div[@class='preview-popup']//a[text()='Preview as Plain Text']");
157 $this->waitForTextPresent("This is text formatted content for Mailing {$mailingName} Webtest.");
158 $this->click("xpath=//button[@title='Close']");
159
160 //--------track and respond----------
161 $this->waitForAjaxContent();
162 $this->click('link=Tracking');
163 $this->assertChecked("url_tracking");
164 $this->assertChecked("open_tracking");
165 // no need tracking for this test
166
167 // default header and footer ( with label )
168 $this->waitForAjaxContent();
169 $this->click('link=Header and Footer');
170 $this->select("header_id", "label=Mailing Header");
171 $this->select("footer_id", "label=Mailing Footer");
172
173 //---------------Test------------------
174
175 ////////--Commenting test mailing and mailing preview (test mailing and preview not presently working).
176
177 // send test mailing
178 //$this->type("test_email", "mailino@mailson.co.in");
179 //$this->click("sendtest");
180
181 // verify status message
182 //$this->assertTrue($this->isTextPresent("Your test message has been sent. Click 'Next' when you are ready to Schedule or Send your live mailing (you will still have a chance to confirm or cancel sending this mailing on the next page)."));
183
184 // check mailing preview
185 //$this->click("//form[@id='Test']/div[2]/div[4]/div[1]");
186 //$this->assertTrue($this->isTextPresent("this is test content for Mailing $mailingName Webtest"));
187
188 ////////
189
190 // click next
191 $this->click("xpath=//div[@class='crm-wizard-buttons']/button[text()='Next']");
192
193 //----------Schedule or Send------------
194
195 // do check for other option
196 $this->waitForTextPresent("Mailing $mailingName Webtest");
197 $this->click("xpath=//div[@class='content']//a[text()='~2 recipients']");
198 $verifyData = array(
199 "$firstUserName Smith" => "$firstUserName.smith@example.org",
200 "$firstName Mailson" => "mailino$firstName@mailson.co.in",
201 );
202 $this->webtestVerifyTabularData($verifyData);
203 $this->waitForTextPresent("(Include: $groupName)");
204 $this->assertChecked("xpath=//input[@id='schedule-send-now']");
205
206 // finally schedule the mail by clicking submit
207 $this->click("xpath=//center/a/div[text()='Submit Mailing']");
208 $this->waitForPageToLoad($this->getTimeoutMsec());
209
210 //----------end New Mailing-------------
211
212 //check redirected page to Scheduled and Sent Mailings and verify for mailing name
213 $this->waitForTextPresent("Find Mailings");
214 $this->assertElementContainsText('Search', "Mailing $mailingName Webtest");
215
216 //--------- mail delivery verification---------
217
218 // test undelivered report
219
220 // click report link of created mailing
221 $this->click("xpath=//table//tbody/tr[td[1]/text()='Mailing $mailingName Webtest']/descendant::a[text()='Report']");
222 $this->waitForPageToLoad($this->getTimeoutMsec());
223
224 // verify undelivered status message
225 $this->assertElementContainsText('crm-container', "Delivery has not yet begun for this mailing. If the scheduled delivery date and time is past, ask the system administrator or technical support contact for your site to verify that the automated mailer task ('cron job') is running - and how frequently.");
226
227 // do check for recipient group
228 $this->assertElementContainsText('crm-container', "Members of $groupName");
229
230 // directly send schedule mailing -- not working right now
231 $this->openCiviPage('mailing/queue', 'reset=1');
232
233 //click report link of created mailing
234 $this->click("xpath=//table//tbody/tr[td[1]/text()='Mailing $mailingName Webtest']/descendant::a[text()='Report']");
235 $this->waitForPageToLoad($this->getTimeoutMsec());
236
237 // do check again for recipient group
238 $this->assertElementContainsText('crm-container', "Members of $groupName");
239
240 // check for 100% delivery
241 $this->assertElementContainsText('crm-container', "2 (100.00%)");
242
243 // verify intended recipients
244 $this->verifyText("xpath=//table//tr[td/a[text()='Intended Recipients']]/descendant::td[2]", preg_quote("2"));
245
246 // verify successful deliveries
247 $this->verifyText("xpath=//table//tr[td/a[text()='Successful Deliveries']]/descendant::td[2]", preg_quote("2 (100.00%)"));
248
249 // verify status
250 $this->verifyText("xpath=//table//tr[td[1]/text()='Status']/descendant::td[2]", preg_quote("Complete"));
251
252 // verify mailing name
253 $this->verifyText("xpath=//table//tr[td[1]/text()='Mailing Name']/descendant::td[2]", preg_quote("Mailing $mailingName Webtest"));
254
255 // verify mailing subject
256 $this->verifyText("xpath=//table//tr[td[1]/text()='Subject']/descendant::td[2]", preg_quote("Test subject $mailingName for Webtest"));
257
258 $this->verifyText("xpath=//table//tr[td[1]/text()='Campaign']/descendant::td[2]", preg_quote("$campaignTitle"));
259
260 //---- check for delivery detail--
261 $this->click("link=Successful Deliveries");
262 $this->waitForPageToLoad($this->getTimeoutMsec());
263
264 // check for open page
265 $this->waitForTextPresent("Successful Deliveries");
266 // verify email
267 $this->assertElementContainsText('mailing_event', "mailino$firstName@mailson.co.in");
268 //------end delivery verification---------
269
270 // Search Advanced Search for contacts associated with Campaign in the Mailings Tab.
271 $this->mailingCampaignAdvancedSearchTest($campaignTitle, $this->_contactNames);
272 }
273
274 public function mailingCampaignAdvancedSearchTest($campaignTitle, $contactNames) {
275 // Go directly to Advanced Search
276 $this->openCiviPage('contact/search/advanced', 'reset=1');
277
278 // Select the Mailing Tab
279 $this->clickAjaxLink("CiviMail", 'campaigns');
280 $this->multiselect2("campaigns", array("$campaignTitle"));
281 $this->click("_qf_Advanced_refresh");
282
283 // Check for contacts inserted while adding Campaing and Mailing
284 $this->waitForElementPresent('search-status');
285 $this->assertElementContainsText('search-status', '2 Contacts');
286 }
287
288 }