<commit_before><?php

namespace Oro\Bundle\FormBundle\Tests\Behat\Context;

use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Doctrine\Common\Inflector\Inflector;
use Oro\Bundle\ConfigBundle\Tests\Behat\Element\SystemConfigForm;
use Oro\Bundle\FormBundle\Tests\Behat\Element\OroForm;
use Oro\Bundle\FormBundle\Tests\Behat\Element\Select;
use Oro\Bundle\FormBundle\Tests\Behat\Element\Select2Entity;
use Oro\Bundle\TestFrameworkBundle\Behat\Context\OroFeatureContext;
use Oro\Bundle\TestFrameworkBundle\Behat\Driver\OroSelenium2Driver;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\CollectionField;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Element;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Form;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\OroPageObjectAware;
use Oro\Bundle\TestFrameworkBundle\Tests\Behat\Context\PageObjectDictionary;

/**
 * @SuppressWarnings(PHPMD.TooManyMethods)
 */
class FormContext extends OroFeatureContext implements OroPageObjectAware
{
    use PageObjectDictionary;

    /**
     * Fill form with data
     * Example: And fill form with:
     *            | Subject     | Simple text     |
     *            | Users       | [Charlie, Pitt] |
     *            | Date        | 2017-08-24      |
     *
     * @When /^(?:|I )fill "(?P<formName>(?:[^"]|\\")*)" with:$/
     * @When /^(?:|I )fill form with:$/
     */
    public function iFillFormWith(TableNode $table, $formName = "OroForm")
    {
        /** @var Form $form */
        $form = $this->createElement($formName);
        $form->fill($table);
    }

    //@codingStandardsIgnoreStart
    /**
     * @When /^(?:|I )open select entity popup for field "(?P<fieldName>[\w\s]*)" in form "(?P<formName>(?:[^"]|\\")*)"$/
     * @When /^(?:|I )open select entity popup for field "(?P<fieldName>[\w\s]*)"$/
     */
    //@codingStandardsIgnoreEnd
    public function iOpenSelectEntityPopup($fieldName, $formName = "OroForm")
    {
        /** @var Select2Entity $field */
        $field = $this->getFieldInForm($fieldName, $formName);
        $field->openSelectEntityPopup();
    }

    /**
     * @When /^(?:|I )clear "(?P<fieldName>[\w\s]*)" field in form "(?P<formName>(?:[^"]|\\")*)"$/
     * @When /^(?:|I )clear "(?P<fieldName>[\w\s]*)" field$/
     */
    public function iClearField($fieldName, $formName = "OroForm")
    {
        /** @var ClearableInterface $field */
        $field = $this->getFieldInForm($fieldName, $formName);

        if (!$field instanceof ClearableInterface) {
            throw new \RuntimeException(sprintf(
                'Element "%s" doesn\'t have ability to clear himself. 
                Behat element "%s" must implement "%s" interface to do this',
                $fieldName,
                is_object($field) ? get_class($field) : gettype($field),
                ClearableInterface::class
            ));
        }

        $field->clear();
    }

    /**
     * @When /^(?:|I )save and close form$/
     */
    public function iSaveAndCloseForm()
    {
        $this->createOroForm()->saveAndClose();
    }

    /**
     * @When /^(?:|I )save and duplicate form$/
     */
    public function iSaveAndDuplicateForm()
    {
        $this->createOroForm()->saveAndDuplicate();
    }

    /**
     * @When /^(?:|I )(save|submit) form$/
     */
    public function iSaveForm()
    {
        $this->createOroForm()->save();
    }

    /**
     * Find and assert field value
     * It's valid for entity edit or entity view page
     * Example: And Account Name field should has Good Company value
     * Example: And Account Name field should has Good Company value
     * Example: And Description field should has Our new partner value
     *
     * @When /^(?P<fieldName>[\w\s]*) field should has (?P<fieldValue>.+) value$/
     */
    public function fieldShouldHaveValue($fieldName, $fieldValue)
    {
        $possibleElementName = $this->fixStepArgument($fieldName);
        if ($this->elementFactory->hasElement($possibleElementName)) {
            $value = $this->createElement($possibleElementName)->getValue();
            self::assertEquals($fieldValue, $value);

            return;
        }

        $page = $this->getSession()->getPage();
        $labels = $page->findAll('css', 'label');

        /** @var NodeElement $label */
        foreach ($labels as $label) {
            if (preg_match(sprintf('/%s/i', $fieldName), $label->getText())) {
                if ($label->hasAttribute('for')) {
                    return $this->getSession()
                        ->getPage()
                        ->find('css', '#' . $label->getAttribute('for'))
                        ->getValue();
                }

                $value = $label->getParent()->find('css', 'div.control-label')->getText();
                self::assertRegExp(sprintf('/%s/i', $fieldValue), $value);<commit_msg>BB-11469: Add comma support for quantity on backend (#12781)<commit_after><?php

namespace Oro\Bundle\FormBundle\Tests\Behat\Context;

use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Doctrine\Common\Inflector\Inflector;
use Oro\Bundle\ConfigBundle\Tests\Behat\Element\SystemConfigForm;
use Oro\Bundle\FormBundle\Tests\Behat\Element\OroForm;
use Oro\Bundle\FormBundle\Tests\Behat\Element\Select;
use Oro\Bundle\FormBundle\Tests\Behat\Element\Select2Entity;
use Oro\Bundle\TestFrameworkBundle\Behat\Context\OroFeatureContext;
use Oro\Bundle\TestFrameworkBundle\Behat\Driver\OroSelenium2Driver;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\CollectionField;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Element;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Form;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\OroPageObjectAware;
use Oro\Bundle\TestFrameworkBundle\Tests\Behat\Context\PageObjectDictionary;

/**
 * @SuppressWarnings(PHPMD.TooManyMethods)
 */
class FormContext extends OroFeatureContext implements OroPageObjectAware
{
    use PageObjectDictionary;

    /**
     * Fill form with data
     * Example: And fill form with:
     *            | Subject     | Simple text     |
     *            | Users       | [<NAME>] |
     *            | Date        | 2017-08-24      |
     *
     * @When /^(?:|I )fill "(?P<formName>(?:[^"]|\\")*)" with:$/
     * @When /^(?:|I )fill form with:$/
     */
    public function iFillFormWith(TableNode $table, $formName = "OroForm")
    {
        /** @var Form $form */
        $form = $this->createElement($formName);
        $form->fill($table);
    }

    //@codingStandardsIgnoreStart
    /**
     * @When /^(?:|I )open select entity popup for field "(?P<fieldName>[\w\s]*)" in form "(?P<formName>(?:[^"]|\\")*)"$/
     * @When /^(?:|I )open select entity popup for field "(?P<fieldName>[\w\s]*)"$/
     */
    //@codingStandardsIgnoreEnd
    public function iOpenSelectEntityPopup($fieldName, $formName = "OroForm")
    {
        /** @var Select2Entity $field */
        $field = $this->getFieldInForm($fieldName, $formName);
        $field->openSelectEntityPopup();
    }

    /**
     * @When /^(?:|I )clear "(?P<fieldName>[\w\s]*)" field in form "(?P<formName>(?:[^"]|\\")*)"$/
     * @When /^(?:|I )clear "(?P<fieldName>[\w\s]*)" field$/
     */
    public function iClearField($fieldName, $formName = "OroForm")
    {
        /** @var ClearableInterface $field */
        $field = $this->getFieldInForm($fieldName, $formName);

        if (!$field instanceof ClearableInterface) {
            throw new \RuntimeException(sprintf(
                'Element "%s" doesn\'t have ability to clear himself. 
                Behat element "%s" must implement "%s" interface to do this',
                $fieldName,
                is_object($field) ? get_class($field) : gettype($field),
                ClearableInterface::class
            ));
        }

        $field->clear();
    }

    /**
     * @When /^(?:|I )save and close form$/
     */
    public function iSaveAndCloseForm()
    {
        $this->createOroForm()->saveAndClose();
    }

    /**
     * @When /^(?:|I )save and duplicate form$/
     */
    public function iSaveAndDuplicateForm()
    {
        $this->createOroForm()->saveAndDuplicate();
    }

    /**
     * @When /^(?:|I )(save|submit) form$/
     */
    public function iSaveForm()
    {
        $this->createOroForm()->save();
    }

    /**
     * Find and assert field value
     * It's valid for entity edit or entity view page
     * Example: And Account Name field should has Good Company value
     * Example: And Account Name field should has Good Company value
     * Example: And Description field should has Our new partner value
     *
     * @When /^(?P<fieldName>[\w\s]*) field should has (?P<fieldValue>.+) value$/
     */
    public function fieldShouldHaveValue($fieldName, $fieldValue)
    {
        $possibleElementName = $this->fixStepArgument($fieldName);
        if ($this->elementFactory->hasElement($possibleElementName)) {
            $value = $this->createElement($possibleElementName)->getValue();
            self::assertSame($fieldValue, $value);

            return;
        }

        $page = $this->getSession()->getPage();
        $labels = $page->findAll('css', 'label');

        /** @var NodeElement $label */
        foreach ($labels as $label) {
            if (preg_match(sprintf('/%s/i', $fieldName), $label->getText())) {
                if ($label->hasAttribute('for')) {
                    return $this->getSession()
                        ->getPage()
                        ->find('css', '#' . $label->getAttribute('for'))
                        ->getValue();
                }

                $value = $label->getParent()->find('css', 'div.control-label')->getText();
                self::assertRegExp(sprintf('/%s/i', $fieldValue), $value);
{'fraction_non_alphanumeric': 0.16086604613516794, 'fraction_numerical': 0.003237555645487657, 'mean_word_length': 2.5442811043384728, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4891449', 'n_tokens_mistral': 3051, 'n_tokens_neox': 2955, 'n_words': 592}