Issues (142)

test/Buy/AbstractBuy.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Test\Buy;
4
5
use Facebook\WebDriver\WebDriverBy;
6
use Facebook\WebDriver\WebDriverExpectedCondition;
7
use Pagantis\ModuleUtils\Exception\NoIdentificationException;
8
use Pagantis\ModuleUtils\Exception\AlreadyProcessedException;
9
use Pagantis\ModuleUtils\Exception\QuoteNotFoundException;
10
use Pagantis\SeleniumFormUtils\SeleniumHelper;
11
use Test\PagantisOscommerceTest;
12
use Httpful\Request;
13
14
/**
15
 * Class AbstractBuy
16
 * @package Test\Buy
17
 */
18
abstract class AbstractBuy extends PagantisOscommerceTest
19
{
20
    /**
21
     * Product name
22
     */
23
    const PRODUCT_NAME = 'Speed';
24
25
    /**
26
     * Correct purchase message
27
     */
28
    const CORRECT_PURCHASE_MESSAGE = 'Su Pedido ha sido Procesado!';
29
30
    /**
31
     * Canceled purchase message
32
     */
33
    const CANCELED_PURCHASE_MESSAGE = 'YOUR ORDER HAS BEEN CANCELED.';
34
35
    /**
36
     * Shopping cart message
37
     */
38
    const SHOPPING_CART_MESSAGE = 'SHOPPING CART';
39
40
    /**
41
     * Empty shopping cart message
42
     */
43
    const EMPTY_SHOPPING_CART = 'SHOPPING CART IS EMPTY';
44
45
    /**
46
     * Pagantis Order Title
47
     */
48
    const PAGANTIS_TITLE = 'Pagantis';
49
50
    /**
51
     * Notification route
52
     */
53
    const NOTIFICATION_FOLDER = '/pagantis/notify';
54
55
    /**
56
     * @param bool $promoted
57
     */
58
    public function prepareProductAndCheckout($promoted = false)
59
    {
60
        $this->goToProductPage();
61
        if ($promoted) {
62
            $this->checkPromoted();
0 ignored issues
show
The method checkPromoted() does not exist on Test\Buy\AbstractBuy. It seems like you code against a sub-type of Test\Buy\AbstractBuy such as Test\Buy\BuyPromotedTest. ( Ignorable by Annotation )

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

62
            $this->/** @scrutinizer ignore-call */ 
63
                   checkPromoted();
Loading history...
63
        }
64
        $this->addToCart();
65
    }
66
67
    /**
68
     * testAddToCart
69
     */
70
    public function addToCart()
71
    {
72
        $addToCartButtonSearch = WebDriverBy::id('tdb4');
73
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($addToCartButtonSearch);
74
        $this->waitUntil($condition);
75
        $addToCartButtonElement = $this->webDriver->findElement($addToCartButtonSearch);
76
        $this->webDriver->executeScript("arguments[0].scrollIntoView(true);", array($addToCartButtonElement));
77
        $addToCartButtonElement->click();
78
79
80
        $buyButtonSearch = WebDriverBy::id('tdb5');
81
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($buyButtonSearch);
82
        $this->waitUntil($condition);
83
        $buyButtonElement = $this->webDriver->findElement($buyButtonSearch);
84
        $this->webDriver->executeScript("arguments[0].scrollIntoView(true);", array($buyButtonElement));
85
        $buyButtonElement->click();
86
    }
87
88
    /**
89
     * Login
90
     */
91
    public function login()
92
    {
93
        $this->findByName('email_address')->clear()->sendKeys($this->configuration['customeremail']);
94
        $this->findByName('password')->clear()->sendKeys($this->configuration['customerpwd']);
95
96
        $buttonSearch = WebDriverBy::id('tdb1');
97
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($buttonSearch);
98
        $this->waitUntil($condition);
99
        $buttonElement = $this->webDriver->findElement($buttonSearch);
100
        $this->webDriver->executeScript("arguments[0].scrollIntoView(true);", array($buttonElement));
101
        $buttonElement->click();
102
    }
103
104
    /**
105
     * Go to the product page
106
     */
107
    public function goToProductPage()
108
    {
109
        $this->webDriver->get(self::OSCURL);
110
        $productGridSearch = WebDriverBy::className('contentContainer');
111
        $productLinkSearch = $productGridSearch->linkText(self::PRODUCT_NAME);
112
113
        $this->webDriver->wait()->until(
114
            WebDriverExpectedCondition::elementToBeClickable(
115
                $productLinkSearch
116
            )
117
        );
118
        $productLinkElement = $this->webDriver->findElement($productLinkSearch);
119
        $this->webDriver->executeScript("arguments[0].scrollIntoView(true);", array($productLinkElement));
120
        sleep(3);
121
        $productLinkElement->click();
122
        $this->assertSame(
123
            self::PRODUCT_NAME . ', ' . self::TITLE,
124
            $this->webDriver->getTitle()
125
        );
126
    }
127
128
    /**
129
     * Fill the shipping method information
130
     */
131
    public function fillPaymentMethod()
132
    {
133
        $button = WebDriverBy::xpath("//tr[@class='moduleRow']/td[2]/input[@value='pagantis']");
134
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($button);
135
        $this->waitUntil($condition);
136
        $this->findByXpath("//tr[@class='moduleRow']/td[2]/input[@value='pagantis']")->click();
137
138
        $buttonSearch = WebDriverBy::id('tdb6');
139
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($buttonSearch);
140
        $this->waitUntil($condition);
141
        $buttonElement = $this->webDriver->findElement($buttonSearch);
142
        $this->webDriver->executeScript("arguments[0].scrollIntoView(true);", array($buttonElement));
143
        $buttonElement->click();
144
    }
145
146
    /**
147
     * Fill the shipping method information
148
     */
149
    public function fillShippingMethod()
150
    {
151
        $buttonSearch = WebDriverBy::id('tdb6');
152
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($buttonSearch);
153
        $this->waitUntil($condition);
154
        $buttonElement = $this->webDriver->findElement($buttonSearch);
155
        $this->webDriver->executeScript("arguments[0].scrollIntoView(true);", array($buttonElement));
156
        $buttonElement->click();
157
    }
158
159
    /**
160
     * Verify Pagantis
161
     *
162
     * @throws \Exception
163
     */
164
    public function verifyPagantis()
165
    {
166
        $condition = WebDriverExpectedCondition::titleContains(self::PAGANTIS_TITLE);
167
        $this->webDriver->wait(300)->until($condition, $this->webDriver->getCurrentURL());
168
        $this->assertTrue((bool)$condition, $this->webDriver->getCurrentURL());
169
    }
170
171
    /**
172
     * Commit Purchase
173
     * @throws \Exception
174
     */
175
    public function commitPurchase()
176
    {
177
178
        $condition = WebDriverExpectedCondition::titleContains(self::PAGANTIS_TITLE);
179
        $this->webDriver->wait(300)->until($condition, $this->webDriver->getCurrentURL());
180
        $this->assertTrue((bool)$condition, "PR32");
181
182
        // complete the purchase with redirect
183
        SeleniumHelper::finishForm($this->webDriver);
184
    }
185
186
187
    /**
188
     * Complete order and open Pagantis (redirect or iframe methods)
189
     *
190
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
191
     * @throws \Facebook\WebDriver\Exception\TimeOutException
192
     */
193
    public function goToPagantis()
194
    {
195
        $buttonSearch = WebDriverBy::id('tdb5');
196
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($buttonSearch);
197
        $this->waitUntil($condition);
198
        $buttonElement = $this->webDriver->findElement($buttonSearch);
199
        $this->webDriver->executeScript("arguments[0].scrollIntoView(true);", array($buttonElement));
200
        $buttonElement->click();
201
    }
202
203
    /**
204
     * Close previous pagantis session if an user is logged in
205
     *
206
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
207
     * @throws \Facebook\WebDriver\Exception\TimeOutException
208
     */
209
    public function logoutFromPagantis()
210
    {
211
        // Wait the page to render (check the simulator is rendered)
212
        $this->webDriver->wait()->until(
213
            WebDriverExpectedCondition::elementToBeClickable(
214
                WebDriverBy::name('minusButton')
215
            )
216
        );
217
        // Check if user is logged in in Pagantis
218
        $closeSession = $this->webDriver->findElements(WebDriverBy::name('one_click_return_to_normal'));
219
        if (count($closeSession) !== 0) {
220
            //Logged out
221
            $continueButtonSearch = WebDriverBy::name('one_click_return_to_normal');
222
            $continueButtonElement = $this->webDriver->findElement($continueButtonSearch);
223
            $continueButtonElement->click();
224
        }
225
    }
226
227
    /**
228
     * Verify That UTF Encoding is working
229
     */
230
    public function verifyUTF8()
231
    {
232
        $paymentFormElement = WebDriverBy::className('FieldsPreview-desc');
233
        $condition = WebDriverExpectedCondition::visibilityOfElementLocated($paymentFormElement);
234
        $this->webDriver->wait()->until($condition);
235
        $this->assertTrue((bool) $condition);
236
        $this->assertSame(
237
            $this->configuration['firstname'] . ' ' . $this->configuration['lastname'],
238
            $this->findByClass('FieldsPreview-desc')->getText()
239
        );
240
    }
241
242
    /**
243
     * Check purchase return message
244
     *
245
     * @param string $message
246
     *
247
     * @throws \Facebook\WebDriver\Exception\NoSuchElementException
248
     * @throws \Facebook\WebDriver\Exception\TimeOutException
249
     */
250
    public function checkPurchaseReturn($message = '')
251
    {
252
        // Check if all goes good
253
        $this->webDriver->wait()->until(
254
            WebDriverExpectedCondition::visibilityOfElementLocated(
255
                WebDriverBy::cssSelector('#bodyContent>h1')
256
            )
257
        );
258
        $successMessage = $this->findByCss('#bodyContent>h1');
259
        $this->assertContains(
260
            $message,
261
            $successMessage->getText()
262
        );
263
    }
264
265
    public function makeValidation()
266
    {
267
        $this->checkConcurrency();
268
        $this->checkPagantisOrderId();
269
        $this->checkAlreadyProcessed();
270
    }
271
272
273
    /**
274
     * Check if with a empty parameter called order-received we can get a QuoteNotFoundException
275
     */
276
    protected function checkConcurrency()
277
    {
278
        $notifyUrl = self::OSCURL.self::NOTIFICATION_FOLDER.'?order=';
279
        $this->assertNotEmpty($notifyUrl, $notifyUrl);
280
        $response = Request::post($notifyUrl)->expects('json')->send();
281
        $this->assertNotEmpty($response->body->result, $response);
282
        $this->assertNotEmpty($response->body->status_code, $response);
283
        $this->assertNotEmpty($response->body->timestamp, $response);
284
        $this->assertContains(
285
            QuoteNotFoundException::ERROR_MESSAGE,
286
            $response->body->result,
287
            "PR=>".$response->body->result
288
        );
289
    }
290
291
    /**
292
     * Check if with a parameter called order-received set to a invalid identification,
293
     * we can get a NoIdentificationException
294
     */
295
    protected function checkPagantisOrderId()
296
    {
297
        $orderId=0;
298
        $notifyUrl = self::OSCURL.self::NOTIFICATION_FOLDER.'?order='.$orderId;
299
        $this->assertNotEmpty($notifyUrl, $notifyUrl);
300
        $response = Request::post($notifyUrl)->expects('json')->send();
301
        $this->assertNotEmpty($response->body->result, $response);
302
        $this->assertNotEmpty($response->body->status_code, $response);
303
        $this->assertNotEmpty($response->body->timestamp, $response);
304
        $this->assertEquals(
305
            $response->body->merchant_order_id,
306
            $orderId,
307
            $response->body->merchant_order_id.'!='. $orderId
308
        );
309
        $this->assertContains(
310
            NoIdentificationException::ERROR_MESSAGE,
311
            $response->body->result,
312
            "PR=>".$response->body->result
313
        );
314
    }
315
316
    /**
317
     * Check if re-launching the notification we can get a AlreadyProcessedException
318
     *
319
     * @throws \Httpful\Exception\ConnectionErrorException
320
     */
321
    protected function checkAlreadyProcessed()
322
    {
323
        $notifyUrl = self::OSCURL.self::NOTIFICATION_FOLDER.'?order=145000008';
324
        $response = Request::post($notifyUrl)->expects('json')->send();
325
        $this->assertNotEmpty($response->body->result, $response);
326
        $this->assertNotEmpty($response->body->status_code, $response);
327
        $this->assertNotEmpty($response->body->timestamp, $response);
328
        $this->assertNotEmpty($response->body->merchant_order_id, $response);
329
        $this->assertNotEmpty($response->body->pagantis_order_id, $response);
330
        $this->assertContains(
331
            AlreadyProcessedException::ERROR_MESSAGE,
332
            $response->body->result,
333
            "PR51=>".$response->body->result
334
        );
335
    }
336
337
}