564254dbf7d54491ed718743196074c56f4e37dd
[tine20] / tests / tine20 / Crm / Import / CsvTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  *
5  * @package     Crm
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2015 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  */
10
11 /**
12  * Test class for Crm_Import_Csv
13  */
14 class Crm_Import_CsvTest extends ImportTestCase
15 {
16     protected $_importerClassName = 'Crm_Import_Csv';
17     protected $_exporterClassName = 'Crm_Export_Csv';
18     protected $_modelName         = 'Crm_Model_Lead';
19
20     protected $_tasksToDelete = array();
21
22     /**
23      * tear down tests
24      */
25     protected function tearDown()
26     {
27         parent::tearDown();
28
29         // delete tasks
30         Tasks_Controller_Task::getInstance()->delete($this->_tasksToDelete);
31
32         Crm_Config::getInstance()->set(Crm_Config::LEAD_IMPORT_AUTOTASK, false);
33         Crm_Config::getInstance()->set(Crm_Config::LEAD_IMPORT_NOTIFICATION, false);
34     }
35     /**
36      * test import
37      *
38      * @param boolean $dryrun
39      * @return array
40      */
41     public function testImport($dryrun = true)
42     {
43         $result = $this->_importHelper('leads.csv', 'crm_tine_import_csv', $dryrun);
44         $this->assertEquals(2, $result['totalcount'], 'should import 2 records: ' . print_r($result, true));
45
46         $firstLead = $result['results']->getFirstRecord();
47         $this->assertContains('neuer lead', $firstLead->lead_name);
48         $this->assertEquals(1, count($firstLead->tags));
49         $this->assertEquals(5, count($firstLead->relations),
50             'relations not imported for first lead ' . print_r($firstLead->toArray(), true));
51         $this->assertEquals(6, count($result['results'][1]->relations),
52             'relations not imported for second lead ' . print_r($result['results'][1]->toArray(), true));
53
54         return $result;
55     }
56
57     /**
58      * import helper
59      *
60      * @param        $importFilename
61      * @param string $definitionName
62      * @param bool   $dryrun
63      * @return array
64      * @throws Tinebase_Exception_NotFound
65      */
66     protected function _importHelper($importFilename, $definitionName = 'crm_tine_import_csv', $dryrun = true)
67     {
68         $this->_testNeedsTransaction();
69
70         $this->_testContainer = $this->_getTestContainer('Crm', 'Crm_Model_Lead');
71         $this->_filename = dirname(__FILE__) . '/files/' . $importFilename;
72         $this->_deleteImportFile = false;
73
74         $options = array(
75             'container_id'  => $this->_testContainer->getId(),
76             'dryrun' => $dryrun,
77         );
78
79         $result = $this->_doImport($options, $definitionName);
80
81         return $result;
82     }
83
84     /**
85      * @see 0011234: automatically add task for responsible person on lead import
86      */
87     public function testAutoTaskImport()
88     {
89         Crm_Config::getInstance()->set(Crm_Config::LEAD_IMPORT_AUTOTASK, true);
90         $personalContainerOfSClever = $this->_getPersonalContainer('Tasks', $this->_personas['sclever']);
91         $this->_setPersonaGrantsForTestContainer($personalContainerOfSClever->getId(), 'sclever', true, false);
92
93         $result = $this->testImport(/* dry run = */ false);
94         foreach ($result['results'] as $lead) {
95             foreach ($lead->relations as $relation) {
96                 if ($relation->type === 'TASK') {
97                     $this->_tasksToDelete[] = $relation->related_id;
98                 }
99             }
100         }
101
102         $tasks = $this->_searchTestTasks($personalContainerOfSClever->getId());
103         $this->assertEquals(1, count($tasks), 'could not find task in sclevers container: '
104             . print_r($personalContainerOfSClever->toArray(), true));
105         $task = $tasks->getFirstRecord();
106         $this->assertEquals($this->_personas['sclever']['accountId'], $task->organizer);
107         $this->assertEquals('IN-PROCESS', $task->status);
108     }
109
110     /**
111      * search tasks
112      *
113      * @param      $containerId
114      * @param null $summary
115      * @return array|Tinebase_Record_RecordSet
116      */
117     protected function _searchTestTasks($containerId, $summary = null)
118     {
119         if (! $summary) {
120             $translate = Tinebase_Translation::getTranslation('Crm');
121             $summary = $translate->_('Edit new lead');
122         }
123         $tasksFilter = new Tasks_Model_TaskFilter(array(
124             array('field' => 'container_id', 'operator' => 'equals', 'value' => $containerId),
125             array('field' => 'summary', 'operator' => 'contains', 'value' => $summary),
126         ));
127         $tasks = Tasks_Controller_Task::getInstance()->search($tasksFilter);
128         $this->_tasksToDelete = array_merge($this->_tasksToDelete, $tasks->getArrayOfIds());
129         return $tasks;
130     }
131
132     /**
133      * @see 0011376: send mail on lead import to responsibles
134      */
135     public function testEmailNotification()
136     {
137         $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
138         if (empty($smtpConfig)) {
139             $this->markTestSkipped('No SMTP config found: this is needed to send notifications.');
140         }
141
142         Crm_Config::getInstance()->set(Crm_Config::LEAD_IMPORT_NOTIFICATION, true);
143         $this->testImport(/* dry run = */ false);
144         // mark tasks for deletion
145         $this->_searchTestTasks(Tinebase_Container::getInstance()->getDefaultContainer('Tasks_Model_Task')->getId(), 'task');
146
147         // assert emails for responsibles
148         $messages = self::getMessages();
149         $this->assertGreaterThan(1, count($messages));
150
151         $translate = Tinebase_Translation::getTranslation('Crm');
152         $importNotifications = array();
153         $subjectToMatch = sprintf($translate->_('%s new leads have been imported'), 1);
154         foreach ($messages as $message) {
155             if ($message->getSubject() == $subjectToMatch) {
156                 $importNotifications[] = $message;
157             }
158         }
159
160         $this->assertGreaterThan(1, count($importNotifications),
161             'expecting 2 or more mails (at least for unittest + sclever) / messages:'
162             . print_r($messages, true));
163         $firstMessage = $importNotifications[0];
164         $this->assertContains('neuer lead 2', $firstMessage->getBodyText()->getContent(), 'lead name missing');
165         $this->assertContains('PHPUnit', $firstMessage->getBodyText()->getContent(), 'container name missing');
166     }
167 }