0011842: import full related record data
[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      * @param string $duplicateResolveStrategy
64      * @return array
65      * @throws Tinebase_Exception_NotFound
66      */
67     protected function _importHelper($importFilename, $definitionName = 'crm_tine_import_csv', $dryrun = true, $duplicateResolveStrategy = null)
68     {
69         $this->_testNeedsTransaction();
70
71         $this->_testContainer = $this->_getTestContainer('Crm', 'Crm_Model_Lead');
72         $this->_filename = dirname(__FILE__) . '/files/' . $importFilename;
73         $this->_deleteImportFile = false;
74
75         $options = array(
76             'container_id'  => $this->_testContainer->getId(),
77             'dryrun' => $dryrun,
78         );
79
80         if ($duplicateResolveStrategy) {
81             $options['duplicateResolveStrategy'] = $duplicateResolveStrategy;
82         }
83
84         $result = $this->_doImport($options, $definitionName);
85
86         return $result;
87     }
88
89     /**
90      * @see 0011234: automatically add task for responsible person on lead import
91      */
92     public function testAutoTaskImport()
93     {
94         Crm_Config::getInstance()->set(Crm_Config::LEAD_IMPORT_AUTOTASK, true);
95         $personalContainerOfSClever = $this->_getPersonalContainer('Tasks', $this->_personas['sclever']);
96         $this->_setPersonaGrantsForTestContainer($personalContainerOfSClever->getId(), 'sclever', true, false);
97
98         $result = $this->testImport(/* dry run = */ false);
99         foreach ($result['results'] as $lead) {
100             foreach ($lead->relations as $relation) {
101                 if ($relation->type === 'TASK') {
102                     $this->_tasksToDelete[] = $relation->related_id;
103                 }
104             }
105         }
106
107         $tasks = $this->_searchTestTasks($personalContainerOfSClever->getId());
108         $this->assertEquals(1, count($tasks), 'could not find task in sclevers container: '
109             . print_r($personalContainerOfSClever->toArray(), true));
110         $task = $tasks->getFirstRecord();
111         $this->assertEquals($this->_personas['sclever']['accountId'], $task->organizer);
112         $this->assertEquals('IN-PROCESS', $task->status);
113     }
114
115     /**
116      * search tasks
117      *
118      * @param      $containerId
119      * @param null $summary
120      * @return array|Tinebase_Record_RecordSet
121      */
122     protected function _searchTestTasks($containerId, $summary = null)
123     {
124         if (! $summary) {
125             $translate = Tinebase_Translation::getTranslation('Crm');
126             $summary = $translate->_('Edit new lead');
127         }
128         $tasksFilter = new Tasks_Model_TaskFilter(array(
129             array('field' => 'container_id', 'operator' => 'equals', 'value' => $containerId),
130             array('field' => 'summary', 'operator' => 'contains', 'value' => $summary),
131         ));
132         $tasks = Tasks_Controller_Task::getInstance()->search($tasksFilter);
133         $this->_tasksToDelete = array_merge($this->_tasksToDelete, $tasks->getArrayOfIds());
134         return $tasks;
135     }
136
137     /**
138      * @see 0011376: send mail on lead import to responsibles
139      */
140     public function testEmailNotification()
141     {
142         $smtpConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::SMTP, new Tinebase_Config_Struct())->toArray();
143         if (empty($smtpConfig)) {
144             $this->markTestSkipped('No SMTP config found: this is needed to send notifications.');
145         }
146
147         Crm_Config::getInstance()->set(Crm_Config::LEAD_IMPORT_NOTIFICATION, true);
148         $this->testImport(/* dry run = */ false);
149         // mark tasks for deletion
150         $this->_searchTestTasks(Tinebase_Container::getInstance()->getDefaultContainer('Tasks_Model_Task')->getId(), 'task');
151
152         // assert emails for responsibles
153         $messages = self::getMessages();
154         $this->assertGreaterThan(1, count($messages));
155
156         $translate = Tinebase_Translation::getTranslation('Crm');
157         $importNotifications = array();
158         $subjectToMatch = sprintf($translate->_('%s new leads have been imported'), 1);
159         foreach ($messages as $message) {
160             if ($message->getSubject() == $subjectToMatch) {
161                 $importNotifications[] = $message;
162             }
163         }
164
165         $this->assertGreaterThan(1, count($importNotifications),
166             'expecting 2 or more mails (at least for unittest + sclever) / messages:'
167             . print_r($messages, true));
168         $firstMessage = $importNotifications[0];
169         $this->assertContains('neuer lead 2', $firstMessage->getBodyText()->getContent(), 'lead name missing');
170         $this->assertContains('PHPUnit', $firstMessage->getBodyText()->getContent(), 'container name missing');
171     }
172 }