Issues (3627)

MauticCrmBundle/Tests/Pipedrive/PipedriveTest.php (1 issue)

1
<?php
2
3
namespace MauticPlugin\MauticCrmBundle\Tests\Pipedrive;
4
5
use Mautic\CoreBundle\Helper\DateTimeHelper;
6
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
7
use Mautic\LeadBundle\Entity\Company;
8
use Mautic\LeadBundle\Entity\Lead;
9
use Mautic\PluginBundle\Entity\Integration;
10
use Mautic\PluginBundle\Entity\IntegrationEntity;
11
use Mautic\PluginBundle\Entity\Plugin;
12
use Mautic\PluginBundle\Helper\IntegrationHelper;
13
use Mautic\UserBundle\Entity\Role;
14
use Mautic\UserBundle\Entity\User;
15
use MauticPlugin\MauticCrmBundle\Entity\PipedriveOwner;
16
use MauticPlugin\MauticCrmBundle\Integration\PipedriveIntegration;
17
18
abstract class PipedriveTest extends MauticMysqlTestCase
19
{
20
    const WEBHOOK_USER     = 'user';
21
    const WEBHOOK_PASSWORD = 'pa$$word';
22
23
    protected function setUp(): void
24
    {
25
        parent::setUp();
26
27
        // Simulate request.
28
        $GLOBALS['requests']        = [];
29
        $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
30
        $_SERVER['SERVER_PORT']     = 80;
31
        $_SERVER['SERVER_NAME']     = 'www.example.com';
32
        $_SERVER['REQUEST_URI']     = '/index.php';
33
    }
34
35
    public function tearDown(): void
36
    {
37
        unset($GLOBALS['requests'], $_SERVER['SERVER_PROTOCOL'], $_SERVER['SERVER_PORT'], $_SERVER['SERVER_NAME']);
38
39
        parent::tearDown();
40
    }
41
42
    /**
43
     * @param $type
44
     *
45
     * @return string
46
     */
47
    public static function getData($type)
48
    {
49
        $filename = dirname(__FILE__).sprintf('/Data/%s.json', $type);
50
        if (file_exists($filename)) {
51
            return file_get_contents($filename);
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * @param string $method
59
     * @param string $json
60
     * @param bool   $addCredential
61
     */
62
    protected function makeRequest($method, $json, $addCredential = true)
63
    {
64
        $headers = !$addCredential ? [] : [
65
            'PHP_AUTH_USER' => self::WEBHOOK_USER,
66
            'PHP_AUTH_PW'   => self::WEBHOOK_PASSWORD,
67
        ];
68
69
        $this->client->request($method, '/plugin/pipedrive/webhook', [], [], $headers, $json);
70
    }
71
72
    protected function installPipedriveIntegration($published = false, array $settings = [], array $apiKeys = ['url' => '', 'token' => ''], array $features = ['push_lead'], $addCredential = true)
73
    {
74
        $plugin = new Plugin();
75
        $plugin->setName('CRM');
76
        $plugin->setDescription('Enables integration with Mautic supported CRMs.');
77
        $plugin->setBundle('MauticCrmBundle');
78
        $plugin->setVersion('1.0');
79
        $plugin->setAuthor('Mautic');
80
81
        $this->em->persist($plugin);
82
83
        $integration = new Integration();
84
        $integration->setName('Pipedrive');
85
        $integration->setIsPublished($published);
86
        $settings = array_merge(
87
            [
88
                'import' => [
89
                    'enabled',
90
                ],
91
            ],
92
            $settings
93
        );
94
        $integration->setFeatureSettings($settings);
95
        $integration->setSupportedFeatures($features);
96
        $integration->setPlugin($plugin);
97
        $this->em->persist($integration);
98
        $this->em->flush();
99
100
        $integrationObject = $this->getIntegrationObject();
101
102
        if ($addCredential) {
103
            [
104
            $apiKeys = array_merge($apiKeys, [
105
                'user'      => self::WEBHOOK_USER,
106
                'password'  => self::WEBHOOK_PASSWORD,
107
            ]),
108
        ];
109
        }
110
111
        $integrationObject->encryptAndSetApiKeys($apiKeys, $integration);
112
113
        $this->em->flush();
114
    }
115
116
    protected function createLead($companies = [], User $owner = null, $data = [])
117
    {
118
        $lead = new Lead();
119
        $lead->setFirstname('Firstname');
120
        $lead->setLastname('Lastname');
121
        $lead->setEmail('[email protected]');
122
        $lead->setPhone('555-666-777');
123
        foreach ($data as $alias => $value) {
124
            $lead->addUpdatedField($alias, $value);
125
        }
126
127
        if ($owner) {
128
            $lead->setOwner($owner);
129
        }
130
131
        $this->em->persist($lead);
132
        $this->em->flush();
133
134
        $companyModel = $this->container->get('mautic.lead.model.company');
135
136
        if ($companies instanceof Company) {
137
            $companies = [$companies];
138
        }
139
140
        foreach ($companies as $company) {
141
            $companyModel->addLeadToCompany($company, $lead);
142
            $lead->setCompany($company->getName());
143
        }
144
        // need modified date due import data to Pipedrive
145
        $lead->setDateModified(new \DateTime('2099-01-01T15:03:01.012345Z'));
146
        $this->em->persist($lead);
147
        $this->em->flush();
148
149
        return $lead;
150
    }
151
152
    protected function createUser($isAdmin = true, $email = '[email protected]', $username = 'pipedrive-admin')
153
    {
154
        $role = new Role();
155
        $role->setName('Test');
156
        $role->setDescription('Test 123');
157
        $role->isAdmin($isAdmin);
0 ignored issues
show
The call to Mautic\UserBundle\Entity\Role::isAdmin() has too many arguments starting with $isAdmin. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        $role->/** @scrutinizer ignore-call */ 
158
               isAdmin($isAdmin);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
158
        $this->em->persist($role);
159
160
        $userModel = $this->client->getContainer()->get('mautic.model.factory')->getModel('user');
161
162
        $user = $userModel->getEntity();
163
        $user->setFirstName('Admin');
164
        $user->setLastName('User');
165
        $user->setUsername($username);
166
        $user->setEmail($email);
167
        $user->setPassword(123456);
168
        $user->setRole($role);
169
170
        $userModel->saveEntity($user);
171
172
        return $user;
173
    }
174
175
    protected function createCompany($name = 'Company Name', $address = 'Wrocław, Poland')
176
    {
177
        $company = new Company();
178
        $company->setName($name);
179
        $company->setAddress1($address);
180
181
        $this->em->persist($company);
182
        $this->em->flush();
183
184
        return $company;
185
    }
186
187
    protected function createLeadIntegrationEntity($integrationEntityId, $internalEntityId)
188
    {
189
        $date = (new DateTimeHelper('2017-05-15 00:00:00'))->getDateTime();
190
191
        $integrationEntity = new IntegrationEntity();
192
193
        $integrationEntity->setDateAdded($date);
194
        $integrationEntity->setLastSyncDate($date);
195
        $integrationEntity->setIntegration(PipedriveIntegration::INTEGRATION_NAME);
196
        $integrationEntity->setIntegrationEntity(PipedriveIntegration::PERSON_ENTITY_TYPE);
197
        $integrationEntity->setIntegrationEntityId($integrationEntityId);
198
        $integrationEntity->setInternalEntity(PipedriveIntegration::LEAD_ENTITY_TYPE);
199
        $integrationEntity->setInternalEntityId($internalEntityId);
200
201
        $this->em->persist($integrationEntity);
202
        $this->em->flush();
203
204
        return $integrationEntity;
205
    }
206
207
    protected function createCompanyIntegrationEntity($integrationEntityId, $internalEntityId)
208
    {
209
        $date = (new DateTimeHelper('2017-05-15 00:00:00'))->getDateTime();
210
211
        $integrationEntity = new IntegrationEntity();
212
213
        $integrationEntity->setDateAdded($date);
214
        $integrationEntity->setLastSyncDate($date);
215
        $integrationEntity->setIntegration(PipedriveIntegration::INTEGRATION_NAME);
216
        $integrationEntity->setIntegrationEntity(PipedriveIntegration::ORGANIZATION_ENTITY_TYPE);
217
        $integrationEntity->setIntegrationEntityId($integrationEntityId);
218
        $integrationEntity->setInternalEntity(PipedriveIntegration::COMPANY_ENTITY_TYPE);
219
        $integrationEntity->setInternalEntityId($internalEntityId);
220
221
        $this->em->persist($integrationEntity);
222
        $this->em->flush();
223
224
        return $integrationEntity;
225
    }
226
227
    protected function getIntegrationObject()
228
    {
229
        /** @var IntegrationHelper $integrationHelper */
230
        $integrationHelper = $this->container->get('mautic.helper.integration');
231
232
        /** @var Integration $integration */
233
        $integration = $integrationHelper->getIntegrationObject(PipedriveIntegration::INTEGRATION_NAME);
234
235
        return $integration;
236
    }
237
238
    protected function addPipedriveOwner($pipedriveOwnerId, $email)
239
    {
240
        $pipedriveOwner = new PipedriveOwner();
241
        $pipedriveOwner->setEmail($email);
242
        $pipedriveOwner->setOwnerId($pipedriveOwnerId);
243
244
        $this->em->persist($pipedriveOwner);
245
        $this->em->flush();
246
247
        return $pipedriveOwner;
248
    }
249
250
    protected function addOwnerToCompany(User $user, Company $company)
251
    {
252
        $company->setOwner($user);
253
254
        $this->em->persist($company);
255
        $this->em->flush();
256
    }
257
}
258