Merge remote-tracking branch 'origin/master' into master-master-civimail-abtest-2014...
[civicrm-core.git] / tests / phpunit / WebTest / Campaign / OfflineContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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_OfflineContributionTest
31 */
32 class WebTest_Campaign_OfflineContributionTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 function testCreateCampaign() {
39 $this->webtestLogin('admin');
40
41 // Create new group
42 $title = substr(sha1(rand()), 0, 7);
43 $groupName = $this->WebtestAddGroup();
44
45 // Adding contact
46 // We're using Quick Add block on the main page for this.
47 $firstName1 = substr(sha1(rand()), 0, 7);
48 $this->webtestAddContact($firstName1, "Smith", "$firstName1.smith@example.org");
49
50 // add contact to group
51 // visit group tab
52 $this->click("css=li#tab_group a");
53 $this->waitForElementPresent("group_id");
54
55 // add to group
56 $this->select("group_id", "label=$groupName");
57 $this->click("_qf_GroupContact_next");
58 $this->waitForElementPresent('link=Remove');
59
60 $firstName2 = substr(sha1(rand()), 0, 7);
61 $this->webtestAddContact($firstName2, "John", "$firstName2.john@example.org");
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 // Enable CiviCampaign module if necessary
74 $this->enableComponents(array('CiviCampaign'));
75
76 // add the required permission
77 $this->changePermissions('edit-2-administer-civicampaign');
78
79 // Log in as normal user
80 $this->webtestLogin();
81
82 $this->openCiviPage('campaign', 'reset=1', "link=Add Campaign");
83 if ($this->isTextPresent('None found.')) {
84 $this->openCiviPage('contribute/add', 'reset=1&action=add&context=standalone', '_qf_Contribution_cancel-bottom');
85 $this->assertElementContainsText('crm-container', 'There are currently no active Campaigns.');
86 }
87 $this->openCiviPage('campaign/add', 'reset=1');
88
89 $campaignTitle = "Campaign $title";
90 $this->type("title", $campaignTitle);
91
92 // select the campaign type
93 $this->select("campaign_type_id", "value=2");
94
95 // fill in the description
96 $this->type("description", "This is a test campaign");
97
98 // include groups for the campaign
99 $this->multiselect2("includeGroups", array("$groupName", "Advisory Board"));
100
101 // fill the end date for campaign
102 $this->webtestFillDate("end_date", "+1 year");
103
104 // select campaign status
105 $this->select("status_id", "value=2");
106
107 // click save
108 $this->clickLink("_qf_Campaign_upload-bottom");
109
110 $this->checkCRMAlert("Campaign $title");
111
112 $this->waitForElementPresent("//td[text()='$campaignTitle']");
113 $campaignId = $this->urlArg('id', $this->getAttribute("//td[text()='$campaignTitle']/../td[13]/span/a[text()='Edit']@href"));
114
115 $this->offlineContributionTest($campaignTitle, $campaignId);
116
117 $this->pastCampaignsTest($groupName);
118 }
119
120 /**
121 * @param $campaignTitle
122 * @param $id
123 * @param bool $past
124 */
125 function offlineContributionTest($campaignTitle, $id, $past = FALSE) {
126 // Create a contact to be used as soft creditor
127 $softCreditFname = substr(sha1(rand()), 0, 7);
128 $softCreditLname = substr(sha1(rand()), 0, 7);
129 $this->webtestAddContact($softCreditFname, $softCreditLname, FALSE);
130
131 // Adding contact with randomized first name (so we can then select that contact when creating contribution.)
132 // We're using Quick Add block on the main page for this.
133 $firstName = substr(sha1(rand()), 0, 7);
134 $this->webtestAddContact($firstName, "Summerson", $firstName . "@summerson.name");
135
136 // go to contribution tab and add contribution.
137 $this->click("css=li#tab_contribute a");
138
139 // wait for Record Contribution elenment.
140 $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
141 $this->click("link=Record Contribution (Check, Cash, EFT ...)");
142
143 $this->waitForElementPresent("_qf_Contribution_cancel-bottom");
144
145 // fill financial type.
146 $this->select("financial_type_id", "Donation");
147
148 // fill in Received Date
149 $this->webtestFillDate('receive_date');
150
151 // source
152 $this->type("source", "Mailer 1");
153
154 if ($past) {
155 $this->click("include-past-campaigns");
156 $this->waitForElementPresent("css=#campaign_id option[value=$id]");
157 }
158
159 $this->select("campaign_id", "value=$id");
160
161 // total amount
162 $this->type("total_amount", "100");
163
164 // select payment instrument type = Check and enter chk number
165 $this->select("payment_instrument_id", "value=4");
166 $this->waitForElementPresent("check_number");
167 $this->type("check_number", "check #1041");
168
169 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
170
171 // soft credit
172 $this->click("xpath=id('Contribution')/div[2]/div[@id='softCredit']/div[1]");
173 $this->type("soft_credit_amount_1", "50");
174 $this->waitForElementPresent("soft_credit_contact_id_1");
175 $this->webtestFillAutocomplete("{$softCreditLname}, {$softCreditFname}", 'soft_credit_contact_id_1');
176
177 //Additional Detail section
178 $this->click("AdditionalDetail");
179 $this->waitForElementPresent("thankyou_date");
180
181 $this->type("note", "Test note for {$firstName}.");
182 $this->type("non_deductible_amount", "10");
183 $this->type("fee_amount", "0");
184 $this->type("net_amount", "0");
185 $this->type("invoice_id", time());
186 $this->webtestFillDate('thankyou_date');
187
188
189 //Premium section
190 $this->click("Premium");
191 $this->waitForElementPresent("fulfilled_date");
192 $this->select("product_name[0]", "label=Coffee Mug ( MUG-101 )");
193 $this->select("product_name[1]", "label=Black");
194 $this->webtestFillDate('fulfilled_date');
195
196 // Clicking save.
197 $this->click("_qf_Contribution_upload-bottom");
198
199 // Is status message correct?
200 $this->checkCRMAlert("The contribution record has been saved.");
201
202 $this->waitForElementPresent("xpath=//*[@id='Search']//div[2]//table[2]/tbody/tr/td[8]/span/a[text()='View']");
203
204 // click through to the Contribution view screen
205 $this->click("xpath=//*[@id='Search']//div[2]//table[2]/tbody/tr/td[8]/span/a[text()='View']");
206 $this->waitForElementPresent('_qf_ContributionView_cancel-bottom');
207
208 // verify Contribution created
209 $this->webtestVerifyTabularData(array('Campaign' => $campaignTitle));
210
211 if ($past) {
212 // when campaign component is disabled
213 $this->openCiviPage('admin/setting/component', 'reset=1', '_qf_Component_next-bottom');
214 $this->addSelection("enableComponents-t", "label=CiviCampaign");
215 $this->click("//option[@value='CiviCampaign']");
216 $this->click("remove");
217 $this->clickLink("_qf_Component_next-bottom");
218
219 $this->checkCRMAlert("Changes Saved");
220
221 $this->openCiviPage('contribute/search', 'reset=1', '_qf_Search_refresh');
222
223 $this->type('sort_name', $firstName);
224 $this->click("_qf_Search_refresh");
225 $this->waitForElementPresent("xpath=//div[@id='contributionSearch']/table/tbody/tr/td[11]/span/a[text()='Edit']");
226 $this->click("xpath=//div[@id='contributionSearch']/table/tbody/tr/td[11]/span/a[text()='Edit']");
227 $this->waitForElementPresent("_qf_Contribution_cancel-bottom");
228 $this->assertTrue($this->isTextPresent("$campaignTitle"));
229 }
230 }
231
232 /**
233 * @param $groupName
234 */
235 function pastCampaignsTest($groupName) {
236 $this->openCiviPage('campaign/add', 'reset=1', '_qf_Campaign_upload-bottom');
237
238 $pastTitle = substr(sha1(rand()), 0, 7);
239 $pastCampaignTitle = "Past Campaign $pastTitle";
240 $this->type("title", $pastCampaignTitle);
241
242 // select the campaign type
243 $this->select("campaign_type_id", "value=2");
244
245 // fill in the description
246 $this->type("description", "This is a test for past campaign");
247
248 // include groups for the campaign
249 $this->multiselect2("includeGroups", array("$groupName", "Advisory Board"));
250
251 // fill the start date for campaign
252 $this->webtestFillDate("start_date", "1 January " . (date('Y') - 1));
253
254 // fill the end date for campaign
255 $this->webtestFillDate("end_date", "31 January " . (date('Y') - 1));
256
257 // select campaign status
258 $this->select("status_id", "value=3");
259
260 // click save
261 $this->clickLink("_qf_Campaign_upload-bottom");
262 $this->checkCRMAlert("Campaign $pastCampaignTitle has been saved.");
263
264 $this->waitForElementPresent("link=Add Campaign");
265 $this->waitForElementPresent("link=Campaigns");
266 $this->click("search_form_campaign");
267 $this->type("campaign_title", $pastCampaignTitle);
268 $this->clickAjaxLink("//a[text()='Search']", "//td[text()='$pastCampaignTitle']");
269 $campaignId = $this->urlArg('id', $this->getAttribute("//td[text()='$pastCampaignTitle']/../td[13]/span/a[text()='Edit']@href"));
270
271 $this->offlineContributionTest($pastCampaignTitle, $campaignId, TRUE);
272 }
273 }
274