/** * @param array $names * @return string * @throws \Exception */ protected function getUniqueName(array $names) { $name = array_unique($names); switch (count($name)) { //normal case case 1: return current($name); //if articleName is too long, it will be cut. So it's different from the other and has to be checked separately //if articleName is too long, it will be cut. So it's different from the other and has to be checked separately case 2: $check = array($name); $result = Helper::checkArray($check); break; default: $result = false; break; } if ($result !== true) { $messages = ['The cart item has different names!']; foreach ($name as $key => $value) { $messages[] = sprintf('"%s" (Key: "%s")', $value, $key); } Helper::throwException($messages); } return $name['articleTitle']; }
/** * Verify if we're on an expected page. Throw an exception if not. * @throws \Exception */ public function verifyPage() { $errors = []; if (!$this->hasSelect('subscribeToNewsletter')) { $errors[] = '- There is no newsletter subscription select!'; } if (!$this->hasField('newsletter')) { $errors[] = '- There is no email field!'; } if (!$this->hasSelect('salutation')) { $errors[] = '- There is no salutation select!'; } if (!$this->hasField('firstname')) { $errors[] = '- There is no firstname field!'; } if (!$this->hasField('lastname')) { $errors[] = '- There is no lastname field!'; } if (!$this->hasField('street')) { $errors[] = '- There is no street field!'; } if (!$this->hasField('zipcode')) { $errors[] = '- There is no zip code field!'; } if (!$this->hasField('city')) { $errors[] = '- There is no city field!'; } if (!$errors) { return; } $message = ['You are not on the newsletter page:']; $message = array_merge($message, $errors); $message[] = 'Current URL: ' . $this->getSession()->getCurrentUrl(); Helper::throwException($message); }
/** * Changes the currency * @param string $currency * @throws \Behat\Mink\Exception\ElementNotFoundException */ public function changeCurrency($currency) { if (!$this->getDriver() instanceof Selenium2Driver) { Helper::throwException('Changing the currency in Responsive template requires Javascript!'); } $valid = ['EUR' => '€ EUR', 'USD' => '$ USD']; $this->selectFieldOption('__currency', $valid[$currency]); }
/** * @param string $language * @return bool * @throws \Exception */ public function verifyPage($language = '') { $info = Helper::getPageInfo($this->getSession(), ['controller', 'action']); if ($info['controller'] === 'checkout' && $info['action'] === 'cart') { return Helper::hasNamedLink($this, 'checkout', $language); } $message = ['You are not on the cart!', 'Current URL: ' . $this->getSession()->getCurrentUrl()]; Helper::throwException($message); return false; }
/** * * @param string $quantity * @param float $amount * @throws \Exception */ public function checkCart($quantity, $amount) { $element = Helper::findElements($this, ['quantity', 'amount']); $check = array('quantity' => array($element['quantity']->getText(), $quantity), 'amount' => Helper::floatArray(array($element['amount']->getText(), $amount))); $result = Helper::checkArray($check); if ($result !== true) { $message = sprintf('The %s of the header cart is wrong! (%s instead of %s)', $result, $check[$result][0], $check[$result][1]); Helper::throwException($message); } }
/** * @inheritdoc */ protected function checkRating(BlogComment $blogComments, $average) { $elements = Helper::findElements($this, ['articleRating', 'articleRatingCount']); $check = ['articleRating' => [$elements['articleRating']->getAttribute('content'), $average], 'articleRatingCount' => [$elements['articleRatingCount']->getText(), count($blogComments)]]; $check = Helper::floatArray($check); $result = Helper::checkArray($check); if ($result !== true) { $message = sprintf('There was a different value of the rating! (%s: "%s" instead of "%s")', $result, $check[$result][0], $check[$result][1]); Helper::throwException($message); } }
/** * @param NotePosition $notePositions * @param array $items */ public function checkNoteProducts(NotePosition $notePositions, array $items) { Helper::assertElementCount($notePositions, count($items)); $result = Helper::searchElements($items, $notePositions); if ($result !== true) { $messages = ['The following articles were not found:']; foreach ($result as $product) { $messages[] = $product['number'] . ' - ' . $product['name']; } Helper::throwException($messages); } }
public function checkCaptcha() { $locators = array('captchaPlaceholder', 'captchaImage', 'captchaHidden'); $element = Helper::findElements($this, $locators); $captchaPlaceholder = $element['captchaPlaceholder']->getAttribute('data-src'); $captchaImage = $element['captchaImage']->getAttribute('src'); $captchaHidden = $element['captchaHidden']->getValue(); if ($captchaPlaceholder !== '/shopware/widgets/Captcha/refreshCaptcha' || strpos($captchaImage, 'data:image/png;base64') === false || empty($captchaHidden)) { $message = 'There is no capture in this form!'; Helper::throwException($message); } }
/** * Checks the evaluations of the current article * @param BlogComment $blogComments * @param $average * @param array $comments * @throws \Exception */ public function checkComments(BlogComment $blogComments, $average, array $comments) { $this->checkRating($blogComments, $average); $comments = Helper::floatArray($comments, ['stars']); $result = Helper::assertElements($comments, $blogComments); if ($result === true) { return; } $messages = array('The following comments are wrong:'); foreach ($result as $evaluation) { $messages[] = sprintf('%s - Bewertung: %s (%s is "%s", should be "%s")', $evaluation['properties']['author'], $evaluation['properties']['stars'], $evaluation['result']['key'], $evaluation['result']['value'], $evaluation['result']['value2']); } Helper::throwException($messages); }
/** * @param NotePosition $notePositions * @param array $items */ public function checkNoteProducts(NotePosition $notePositions, array $items) { if (count($notePositions) !== count($items)) { $message = sprintf('There are %d products on the note! (should be %d)', count($notePositions), count($items)); Helper::throwException($message); } $result = Helper::searchElements($items, $notePositions); if ($result !== true) { $messages = array('The following articles were not found:'); foreach ($result as $product) { $messages[] = $product['number'] . ' - ' . $product['name']; } Helper::throwException($messages); } }
/** * Checks if the robots meta exists and matches the expected content * * @param $content */ public function checkRobots($content = []) { $elements = Helper::findElements($this, ['robots']); $robotsElement = $elements['robots']; $robotsValue = $robotsElement->getAttribute('content'); $robotsParts = explode(',', $robotsValue); $robotsParts = array_map('trim', $robotsParts); if (empty($robotsParts)) { Helper::throwException(['Missing robots data']); } if ($robotsParts != $content) { $message = sprintf('Canonical link "%s" does not match expected value "%s"', implode(', ', $robotsParts), implode(', ', $content)); Helper::throwException([$message]); } }
/** * Checks, whether a captcha exists and has loaded correctly * @throws \Exception */ public function checkCaptcha() { $placeholderSelector = Helper::getRequiredSelector($this, 'captchaPlaceholder'); if (!$this->getSession()->wait(5000, "\$('{$placeholderSelector}').children().length > 0")) { $message = 'The captcha was not loaded or does not exist!'; Helper::throwException($message); } $element = Helper::findElements($this, ['captchaPlaceholder', 'captchaImage', 'captchaHidden']); $captchaPlaceholder = $element['captchaPlaceholder']->getAttribute('data-src'); $captchaImage = $element['captchaImage']->getAttribute('src'); $captchaHidden = $element['captchaHidden']->getValue(); if (strpos($captchaPlaceholder, '/widgets/Captcha/refreshCaptcha') === false || strpos($captchaImage, 'data:image/png;base64') === false || empty($captchaHidden)) { $message = 'The captcha was not loaded correctly!'; Helper::throwException($message); } }
/** * @inheritdoc */ public function verifyPage() { if (Helper::hasNamedLink($this, 'moreProducts')) { return; } $errors = []; if (!$this->hasLink('Filtern')) { $errors[] = '- There is no filter link!'; } if (!$this->hasSelect('o')) { $errors[] = '- There is no order select!'; } if (!$errors) { return; } $message = ['You are not on a listing:']; $message = array_merge($message, $errors); $message[] = 'Current URL: ' . $this->getSession()->getCurrentUrl(); Helper::throwException($message); }
protected function checkRating(MultipleElement $articleEvaluations, $average) { $locators = array('productRating', 'productRatingCount'); $elements = Helper::findElements($this, $locators); $check = array(); foreach ($elements as $locator => $element) { switch ($locator) { case 'productRating': $rating = $element->getAttribute('content'); $rating = floatval($rating); $check[$locator] = array($rating, $average); break; case 'productRatingCount': $check[$locator] = array($element->getText(), count($articleEvaluations)); break; } } $check = Helper::floatArray($check); $result = Helper::checkArray($check); if ($result !== true) { $message = sprintf('There was a different value of the evaluation! (%s: "%s" instead of %s)', $result, $check[$result][0], $check[$result][1]); Helper::throwException($message); } }
/** * @param string $configName * @param mixed $value */ public function changeConfigValue($configName, $value) { /** @var Connection $dbal */ $dbal = $this->getService('dbal_connection'); $configId = $dbal->fetchColumn('SELECT `id` FROM `s_core_config_elements` WHERE `name` = ?', [$configName]); if (!$configId) { $message = sprintf('Configuration "%s" doesn\'t exist!', $configName); Helper::throwException($message); } $this->dirtyConfigElements[] = $configId; /** @var \Shopware\Components\ConfigWriter $configWriter */ $configWriter = $this->getService('config_writer'); $configWriter->save($configName, $value, null, 1); $configWriter->save($configName, $value, null, 2); $config = $this->getService('config'); $config->offsetSet($configName, $value); $this->clearCache(); }
/** * @param array $links * @throws \Exception */ public function checkXml(array $links) { $homepageUrl = rtrim($this->getParameter('base_url'), '/'); $xml = new \SimpleXMLElement($this->getContent()); $check = []; $i = 0; foreach ($xml as $link) { if (empty($links[$i])) { $messages = ['There are more links in the sitemap.xml as expected!', sprintf('(%d sites in sitemap.xml, %d in test data', count($xml), count($links))]; Helper::throwException($messages); } $check[] = [(string) $link->loc, $homepageUrl . $links[$i]['link']]; $i++; } $result = Helper::checkArray($check, true); if ($result === true) { return; } $messages = ['A link is different!', 'Read: ' . $check[$result][0], 'Expected: ' . $check[$result][1]]; Helper::throwException($messages); }
/** * Checks an emotion article element * @param Article $article * @param array $data */ public function checkArticle(Article $article, array $data) { $properties = Helper::convertTableHashToArray($data); $properties = Helper::floatArray($properties, ['price']); $result = Helper::assertElementProperties($article, $properties); if ($result === true) { return; } $message = sprintf('The article %s is "%s" (should be "%s")', $result['key'], $result['value'], $result['value2']); Helper::throwException($message); }
/** * @param string $configName * @param mixed $value * @throws \Exception */ public function changeConfigValue($configName, $value) { //get the template id $sql = sprintf('SELECT `id` FROM `s_core_config_elements` WHERE `name` = "%s"', $configName); $configId = $this->getContainer()->get('db')->fetchOne($sql); if (!$configId) { $message = sprintf('Configuration "%s" doesn\'t exist!', $configName); Helper::throwException($message); } $this->dirtyConfigElements[] = $configId; /** @var \Shopware\Components\ConfigWriter $configWriter */ $configWriter = $this->getContainer()->get('config_writer'); $configWriter->save($configName, $value, null, 1); $configWriter->save($configName, $value, null, 2); $this->clearCache(); }
/** * @param string $locator * @param bool $throwException * @return bool */ public function noElement($locator, $throwException = true) { if (Helper::getRequiredSelector($this, $locator)) { //previous or next $result = Helper::countElements($this, $locator); } else { //page number (1, 2, 3, 4, ...) $result = !$this->hasLink($locator); } if ($result === true) { return true; } if ($throwException) { $message = sprintf('The Paging Link "%s" exists, but should not!', $locator); Helper::throwException($message); } return false; }
/** * @param string $paymentMethod * @throws \Behat\Behat\Exception\PendingException * @throws \Exception */ public function checkPaymentMethod($paymentMethod) { /** @var CheckoutPayment $element */ $element = $this->getElement('CheckoutPayment'); $properties = array('paymentMethod' => $paymentMethod); $result = Helper::assertElementProperties($element, $properties); if ($result === true) { return; } $message = sprintf('The current payment method is "%s" (should be "%s")', $result['value'], $result['value2']); Helper::throwException($message); }
/** * Checks the cart positions * Available properties are: number (required), name (required), quantity, itemPrice, sum * @param CartPosition $cartPositions * @param array $items */ public function checkCartProducts(CartPosition $cartPositions, array $items) { if (count($cartPositions) !== count($items)) { $message = sprintf('There are %d products in the cart! (should be %d)', count($cartPositions), count($items)); Helper::throwException($message); } $items = Helper::floatArray($items, ['quantity', 'itemPrice', 'sum']); $result = Helper::assertElements($items, $cartPositions); if ($result !== true) { $messages = array('The following articles are wrong:'); foreach ($result as $product) { $messages[] = sprintf('%s - %s (%s is "%s", should be "%s")', $product['properties']['number'], $product['properties']['name'], $product['result']['key'], $product['result']['value'], $product['result']['value2']); } Helper::throwException($messages); } }
/** * Checks a select box * @param string $select Name of the select box * @param string $min First option * @param string $max Last option * @param integer $graduation Steps between each options * @throws \Exception */ public function checkSelect($select, $min, $max, $graduation) { $selectBox = $this->findField($select); $min = strval($min); $max = strval($max); if (empty($selectBox)) { $message = sprintf('Select box "%s" was not found!', $select); Helper::throwException($message); } $options = $selectBox->findAll('css', 'option'); $errors = []; $optionText = $options[0]->getText(); $parts = explode(' ', $optionText, 2); $value = $parts[0]; $unit = isset($parts[1]) ? ' ' . $parts[1] : ''; if ($optionText !== $min) { $errors[] = sprintf('The first option of "%s" is "%s"! (should be "%s")', $select, $optionText, $min); } /** @var NodeElement $option */ while ($option = next($options)) { $optionText = $option->getText(); $value += $graduation; if ($optionText !== $value . $unit) { $errors[] = sprintf('There is the invalid option "%s" in "%s"! ("%s" expected)', $optionText, $select, $value . $unit); } } if ($optionText !== $max) { $errors[] = sprintf('The last option of "%s" is "%s"! (should be "%s")', $select, $value, $max); } if (!empty($errors)) { Helper::throwException($errors); } }
/** * @param AddressBox $addresses * @param string $name * @throws \Exception */ protected function searchAddress(AddressBox $addresses, $name) { /** @var AddressBox $address */ foreach ($addresses as $address) { if (strpos($address->getProperty('title'), $name) === false) { continue; } $language = Helper::getCurrentLanguage($this); Helper::pressNamedButton($address, 'chooseButton', $language); return; } $messages = array('The address "' . $name . '" is not available. Available are:'); /** @var AddressBox $address */ foreach ($addresses as $address) { $messages[] = $address->getProperty('title'); } Helper::throwException($messages); }
/** * Verify if we're on an expected page. Throw an exception if not. * @return bool * @throws \Exception */ public function verifyPage() { try { $assert = new WebAssert($this->getSession()); $assert->pageTextContains('1 Ihr Warenkorb 2 Ihre Adresse 3 Prüfen und Bestellen'); } catch (ResponseTextException $e) { $message = ['You are not on the cart!', 'Current URL: ' . $this->getSession()->getCurrentUrl()]; Helper::throwException($message); } return Helper::hasNamedLink($this, 'checkout'); }
/** * Global method to check the count of an MultipleElement * @param MultipleElement $elements * @param int $count */ public static function assertElementCount(MultipleElement $elements, $count = 0) { if ($count !== count($elements)) { $message = sprintf('There are %d elements of type "%s" on page (should be %d)', count($elements), get_class($elements), $count); MinkHelper::throwException($message); } }