*/
public $membershipTypeID;
+ /**
+ * Report instance used in mail_report tests.
+ * @var array
+ */
+ private $report_instance;
+
/**
* Set up for tests.
*/
'parameters' => 'Semi-formal explanation of runtime job parameters',
'is_active' => 1,
];
+ $this->report_instance = $this->createReportInstance();
}
/**
return [$membershipTypeID, $groupID, $theChosenOneID, $provider];
}
+ /**
+ * Test that the mail_report job sends an email for 'print' format.
+ *
+ * We're not testing that the report itself is correct since in 'print'
+ * format it's a little difficult to parse out, so we're just testing that
+ * the email was sent and it more or less looks like an email we'd expect.
+ */
+ public function testMailReportForPrint() {
+ $mut = new CiviMailUtils($this, TRUE);
+
+ // avoid warnings
+ if (empty($_SERVER['QUERY_STRING'])) {
+ $_SERVER['QUERY_STRING'] = 'reset=1';
+ }
+
+ $this->callAPISuccess('job', 'mail_report', [
+ 'instanceId' => $this->report_instance['id'],
+ 'format' => 'print',
+ ]);
+
+ $message = $mut->getMostRecentEmail('ezc');
+
+ $this->assertEquals('This is the email subject', $message->subject);
+ $this->assertEquals('reportperson@example.com', $message->to[0]->email);
+
+ $parts = $message->fetchParts(NULL, TRUE);
+ $this->assertCount(1, $parts);
+ $this->assertStringContainsString('test report', $parts[0]->text);
+
+ $mut->clearMessages();
+ $mut->stop();
+ }
+
+ /**
+ * Test that the mail_report job sends an email for 'pdf' format.
+ *
+ * We're not testing that the report itself is correct since in 'pdf'
+ * format it's a little difficult to parse out, so we're just testing that
+ * the email was sent and it more or less looks like an email we'd expect.
+ */
+ public function testMailReportForPdf() {
+ $mut = new CiviMailUtils($this, TRUE);
+
+ // avoid warnings
+ if (empty($_SERVER['QUERY_STRING'])) {
+ $_SERVER['QUERY_STRING'] = 'reset=1';
+ }
+
+ $this->callAPISuccess('job', 'mail_report', [
+ 'instanceId' => $this->report_instance['id'],
+ 'format' => 'pdf',
+ ]);
+
+ $message = $mut->getMostRecentEmail('ezc');
+
+ $this->assertEquals('This is the email subject', $message->subject);
+ $this->assertEquals('reportperson@example.com', $message->to[0]->email);
+
+ $parts = $message->fetchParts(NULL, TRUE);
+ $this->assertCount(2, $parts);
+ $this->assertStringContainsString('<title>CiviCRM Report</title>', $parts[0]->text);
+ $this->assertEquals(ezcMailFilePart::CONTENT_TYPE_APPLICATION, $parts[1]->contentType);
+ $this->assertEquals('pdf', $parts[1]->mimeType);
+ $this->assertEquals(ezcMailFilePart::DISPLAY_ATTACHMENT, $parts[1]->dispositionType);
+ $this->assertGreaterThan(0, filesize($parts[1]->fileName));
+
+ $mut->clearMessages();
+ $mut->stop();
+ }
+
+ /**
+ * Test that the mail_report job sends an email for 'csv' format.
+ *
+ * As with the print and pdf we're not super-concerned about report
+ * functionality itself - we're more concerned with the mailing part,
+ * but since it's csv we can easily check the output.
+ */
+ public function testMailReportForCsv() {
+ // Create many contacts, in particular so that the report would be more
+ // than a one-pager.
+ for ($i = 0; $i < 110; $i++) {
+ $this->individualCreate([], $i, TRUE);
+ }
+
+ $mut = new CiviMailUtils($this, TRUE);
+
+ // avoid warnings
+ if (empty($_SERVER['QUERY_STRING'])) {
+ $_SERVER['QUERY_STRING'] = 'reset=1';
+ }
+
+ $this->callAPISuccess('job', 'mail_report', [
+ 'instanceId' => $this->report_instance['id'],
+ 'format' => 'csv',
+ ]);
+
+ $message = $mut->getMostRecentEmail('ezc');
+
+ $this->assertEquals('This is the email subject', $message->subject);
+ $this->assertEquals('reportperson@example.com', $message->to[0]->email);
+
+ $parts = $message->fetchParts(NULL, TRUE);
+ $this->assertCount(2, $parts);
+ $this->assertStringContainsString('<title>CiviCRM Report</title>', $parts[0]->text);
+ $this->assertEquals('csv', $parts[1]->subType);
+
+ // Pull all the contacts to get our expected output.
+ $contacts = $this->callAPISuccess('Contact', 'get', [
+ 'return' => 'sort_name',
+ 'options' => [
+ 'limit' => 0,
+ 'sort' => 'sort_name',
+ ],
+ ]);
+ $rows = [];
+ foreach ($contacts['values'] as $contact) {
+ $rows[] = ['civicrm_contact_sort_name' => $contact['sort_name']];
+ }
+ // need this for makeCsv()
+ $fakeForm = new CRM_Report_Form();
+ $fakeForm->_columnHeaders = [
+ 'civicrm_contact_sort_name' => [
+ 'title' => 'Contact Name',
+ 'type' => 2,
+ ],
+ ];
+
+ $this->assertEquals(
+ CRM_Report_Utils_Report::makeCsv($fakeForm, $rows),
+ $parts[1]->text
+ );
+
+ $mut->clearMessages();
+ $mut->stop();
+ }
+
+ /**
+ * Helper to create a report instance of the contact summary report.
+ */
+ private function createReportInstance() {
+ return $this->callAPISuccess('ReportInstance', 'create', [
+ 'report_id' => 'contact/summary',
+ 'title' => 'test report',
+ 'form_values' => [
+ serialize([
+ 'fields' => [
+ 'sort_name' => '1',
+ 'street_address' => '1',
+ 'city' => '1',
+ 'country_id' => '1',
+ ],
+ 'sort_name_op' => 'has',
+ 'sort_name_value' => '',
+ 'source_op' => 'has',
+ 'source_value' => '',
+ 'id_min' => '',
+ 'id_max' => '',
+ 'id_op' => 'lte',
+ 'id_value' => '',
+ 'country_id_op' => 'in',
+ 'country_id_value' => [],
+ 'state_province_id_op' => 'in',
+ 'state_province_id_value' => [],
+ 'gid_op' => 'in',
+ 'gid_value' => [],
+ 'tagid_op' => 'in',
+ 'tagid_value' => [],
+ 'description' => 'Provides a list of address and telephone information for constituent records in your system.',
+ 'email_subject' => 'This is the email subject',
+ 'email_to' => 'reportperson@example.com',
+ 'email_cc' => '',
+ 'permission' => 'view all contacts',
+ 'groups' => '',
+ 'domain_id' => 1,
+ ]),
+ ],
+ // Email params need to be repeated outside form_values for some reason
+ 'email_subject' => 'This is the email subject',
+ 'email_to' => 'reportperson@example.com',
+ ]);
+ }
+
}