Completed
Push — master ( 345759...43fb10 )
by
unknown
14:22
created

accountsInTheControlAreFilteredBySelected()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Tests\Behat\Context;
4
5
use Behat\Mink\Element\NodeElement;
6
use Behat\Symfony2Extension\Context\KernelAwareContext;
7
use Behat\Symfony2Extension\Context\KernelDictionary;
8
use Doctrine\ORM\EntityRepository;
9
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\Grid;
10
use Oro\Bundle\FormBundle\Tests\Behat\Element\OroForm;
11
use Oro\Bundle\FormBundle\Tests\Behat\Element\Select2Entity;
12
use Oro\Bundle\NavigationBundle\Tests\Behat\Element\MainMenu;
13
use Oro\Bundle\TestFrameworkBundle\Behat\Context\OroFeatureContext;
14
use Oro\Bundle\TestFrameworkBundle\Behat\Element\OroElementFactoryAware;
15
use Oro\Bundle\TestFrameworkBundle\Behat\Fixtures\FixtureLoaderAwareInterface;
16
use Oro\Bundle\TestFrameworkBundle\Behat\Fixtures\FixtureLoaderDictionary;
17
use Oro\Bundle\TestFrameworkBundle\Tests\Behat\Context\ElementFactoryDictionary;
18
use Oro\Bundle\UserBundle\Entity\User;
19
use OroCRM\Bundle\AccountBundle\Entity\Account;
20
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
21
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomer;
22
23
class FeatureContext extends OroFeatureContext implements
0 ignored issues
show
Bug introduced by
There is one abstract method setFixtureLoader in this class; you could implement it, or declare this class as abstract.
Loading history...
24
    FixtureLoaderAwareInterface,
25
    OroElementFactoryAware,
26
    KernelAwareContext
27
{
28
    use FixtureLoaderDictionary, ElementFactoryDictionary, KernelDictionary;
29
30
    /**
31
     * @Given /^(?:|I )open (Opportunity) creation page$/
32
     */
33
    public function openOpportunityCreationPage()
34
    {
35
        /** @var MainMenu $menu */
36
        $menu = $this->createElement('MainMenu');
37
        $menu->openAndClick('Sales/ Opportunities');
38
        $this->waitForAjax();
39
        $this->getPage()->clickLink('Create Opportunity');
40
    }
41
42
    /**
43
     * @Given /^"(?P<channelName>([\w\s]+))" is a channel with enabled (?P<entities>(.+)) entities$/
44
     */
45
    public function createChannelWithEnabledEntities($channelName, $entities)
46
    {
47
        /** @var MainMenu $menu */
48
        $menu = $this->createElement('MainMenu');
49
        $menu->openAndClick('System/ Channels');
50
        $this->waitForAjax();
51
        $this->getPage()->clickLink('Create Channel');
52
        $this->waitForAjax();
53
54
        /** @var OroForm $form */
55
        $form = $this->createElement('OroForm');
56
        $form->fillField('Name', $channelName);
57
        $form->fillField('Channel Type', 'Sales');
58
        $this->waitForAjax();
59
60
        /** @var Grid $grid */
61
        $grid = $this->createElement('Grid');
62
        $channelEntities = array_map('trim', explode(',', $entities));
63
        $rowsForDelete = [];
64
65
        foreach ($grid->getRows() as $row) {
66
            foreach ($channelEntities as $key => $channelEntity) {
67
                if (false !== stripos($row->getText(), $channelEntity)) {
68
                    unset($channelEntities[$key]);
69
                    continue 2;
70
                }
71
            }
72
73
            $rowsForDelete[] = $row;
74
        }
75
76
        foreach ($rowsForDelete as $row) {
77
            $grid->getActionLink('Delete', $row)->click();
78
        }
79
80
        $entitySelector = $this->elementFactory->findElementContains('EntitySelector', 'Please select entity');
81
82
        foreach ($channelEntities as $channelEntity) {
83
            $entitySelector->click();
84
            $this->elementFactory->findElementContains('SelectToResultLabel', $channelEntity)->click();
85
            $this->getPage()->clickLink('Add');
86
        }
87
88
        $form->saveAndClose();
89
    }
90
91
    /**
92
     * @Given they has their own Accounts and Business Customers
93
     */
94
    public function accountHasBusinessCustomers()
95
    {
96
        $this->fixtureLoader->loadFixtureFile('accounts_with_customers.yml');
97
    }
98
99
    /**
100
     * @Given /^two users (?P<user1>(\w+)) and (?P<user2>(\w+)) exists in the system$/
101
     */
102
    public function twoUsersExistsInTheSystem()
103
    {
104
        $this->fixtureLoader->loadFixtureFile('users.yml');
105
    }
106
107
    /**
108
     * @Then /^Accounts and Customers in the control are filtered according to (?P<user>(\w+)) ACL permissions$/
109
     */
110
    public function accountsInTheControlAreFilteredAccordingToUserAclPermissions($username)
111
    {
112
        $doctrine = $this->getContainer()->get('oro_entity.doctrine_helper');
113
        $owner = $doctrine->getEntityRepositoryForClass(User::class)->findOneBy(['username' => $username]);
114
        $ownAccounts = $doctrine->getEntityRepositoryForClass(B2bCustomer::class)->findBy(['owner' => $owner]);
115
116
        /** @var Select2Entity $accountField */
117
        $accountField = $this->createElement('OroForm')->findField('Account');
118
        $visibleAccounts = $accountField->getSuggestedValues();
119
120
        self::assertCount(count($ownAccounts), $visibleAccounts);
121
122
        /** @var B2bCustomer $account */
123
        foreach ($ownAccounts as $account) {
124
            $value = sprintf('%s (%s)', $account->getName(), $account->getAccount()->getName());
125
            self::assertContains($value, $visibleAccounts);
126
        }
127
    }
128
129
    /**
130
     * @Given CRM has second sales channel with Accounts and Business Customers
131
     */
132
    public function crmHasSecondSalesChannel()
133
    {
134
        $this->fixtureLoader->loadFixtureFile('second_sales_channel.yml');
135
    }
136
137
    /**
138
     * @Then Accounts and Customers in the control are filtered by selected sales channel and :username ACL permissions
139
     */
140
    public function accountsInTheControlAreFilteredBySelected($username)
141
    {
142
        /** @var Select2Entity $channelField */
143
        $channelField = $this->createElement('OroForm')->findField('Channel');
144
        $channels = $channelField->getSuggestedValues();
145
146
        foreach ($channels as $channelName) {
147
            $channelField->setValue($channelName);
148
149
            $expectedCustomers = $this->getCustomers($channelName, $username);
150
151
            /** @var Select2Entity $accountField */
152
            $accountField = $this->createElement('OroForm')->findField('Account');
153
            $actualCustomers = $accountField->getSuggestedValues();
154
155
            self::assertEquals(
156
                sort($expectedCustomers),
157
                sort($actualCustomers)
158
            );
159
        }
160
    }
161
162
    /**
163
     * @Given Account Name is equal to Business Customer name
164
     */
165
    public function accountNameEqualToBusinessCustomer()
166
    {
167
        $this->fixtureLoader->loadFixtureFile('account_name_equal_to_business_customer_name.yml');
168
    }
169
170
    /**
171
     * @Then /^I see only Account name in Account\/Customer field choice$/
172
     */
173
    public function iSeeAccountNameOnly()
174
    {
175
        /** @var Select2Entity $accountField */
176
        $accountField = $this->createElement('OroForm')->findField('Account');
177
        $actualCustomers = $accountField->getSuggestedValues();
178
179
        self::assertContains('Samantha Customer', $actualCustomers);
180
        self::assertNotContains('Samantha Customer (Samantha Customer)', $actualCustomers);
181
    }
182
183
    /**
184
     * @Given Account :name has no customers
185
     */
186
    public function accountHasNoCustomers($name)
187
    {
188
        $this->fixtureLoader->load([
189
            Account::class => [
190
                uniqid('account_', true) => [
191
                    'name' => $name,
192
                    'owner' => '@samantha',
193
                    'organization' => '@organization'
194
                ]
195
            ]
196
        ]);
197
    }
198
199
    /**
200
     * @When I select :name
201
     */
202
    public function selectAccount($name)
203
    {
204
        /** @var Select2Entity $accountField */
205
        $accountField = $this->createElement('OroForm')->findField('Account');
206
        $accountField->fillSearchField($name);
207
        $results = $accountField->getSuggestions();
208
        foreach ($results as $result) {
209
            if (false !== stripos($result->getText(), $name)) {
210
                $result->click();
211
212
                return;
213
            }
214
        }
215
        self::fail('Not found account in suggested variants');
216
    }
217
218
    /**
219
     * @Then :content Customer was created
220
     */
221
    public function customerWasCreated($content)
222
    {
223
        /** @var MainMenu $menu */
224
        $menu = $this->createElement('MainMenu');
225
        $menu->openAndClick('Customers/ Business Customers');
226
        $this->waitForAjax();
227
228
        $this->assertRowInGrid($content);
229
    }
230
231
    /**
232
     * @Then :content Account was created
233
     */
234
    public function accountWasCreated($content)
235
    {
236
        /** @var MainMenu $menu */
237
        $menu = $this->createElement('MainMenu');
238
        $menu->openAndClick('Customers/ Accounts');
239
        $this->waitForAjax();
240
241
        $this->assertRowInGrid($content);
242
    }
243
244
    /**
245
     * @param string $content
246
     */
247
    private function assertRowInGrid($content)
248
    {
249
        $row = $this->elementFactory
250
            ->findElementContains('Grid', $content)
251
            ->findElementContains('GridRow', $content);
252
253
        self::assertTrue($row->isValid(), "Can't find '$content' in grid");
254
    }
255
256
    /**
257
     * @When type :text into Account field
258
     */
259
    public function iTypeIntoAccountField($text)
260
    {
261
        /** @var Select2Entity $accountField */
262
        $accountField = $this->createElement('OroForm')->findField('Account');
263
        $accountField->fillSearchField($text);
264
    }
265
266
    /**
267
     * @Then I should see only existing accounts
268
     */
269
    public function iShouldSeeOnlyExistingAccounts()
270
    {
271
        $existingCustomers = $this->getCustomers('First Sales Channel', 'samantha');
272
273
        /** @var Select2Entity $accountField */
274
        $accountField = $this->createElement('OroForm')->findField('Account');
275
        $actualCustomers = $accountField->getSuggestedValues();
276
277
        self::assertEquals(
278
            sort($existingCustomers),
279
            sort($actualCustomers)
280
        );
281
    }
282
283
    /**
284
     * @Then should not see :text account
285
     */
286
    public function shouldNotSeeAccount($text)
287
    {
288
        /** @var Select2Entity $accountField */
289
        $accountField = $this->createElement('OroForm')->findField('Account');
290
        $actualCustomers = $accountField->getSuggestedValues();
291
292
        self::assertNotContains($text, $actualCustomers);
293
    }
294
295
    /**
296
     * @param string $channelName
297
     * @param string $username
298
     * @return array
299
     */
300
    private function getCustomers($channelName, $username)
301
    {
302
        $doctrine = $this->getContainer()->get('oro_entity.doctrine_helper');
303
        $customerRepository = $doctrine->getEntityManagerForClass(B2bCustomer::class)
304
            ->getRepository(B2bCustomer::class);
305
        $channelRepository = $doctrine->getEntityManagerForClass(Channel::class)->getRepository(Channel::class);
306
307
        $user = $doctrine->getEntityManagerForClass(User::class)->getRepository(User::class)
308
            ->findOneBy(['username' => $username]);
309
        $channel = $channelRepository->findOneBy(['name' => $channelName]);
310
311
        $customers = [];
312
313
        /** @var B2bCustomer $customer */
314
        foreach ($customerRepository->findBy(['owner' => $user, 'dataChannel' => $channel]) as $customer) {
315
            $customers[] = sprintf('%s (%s)', $customer->getName(), $customer->getAccount()->getName());
316
        }
317
318
        return $customers;
319
    }
320
}
321