<commit_before><?php

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

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;

use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridColumnManager;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridToolBarTools;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\MultipleChoice;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\Grid;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridFilterDateTimeItem;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridFilters;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridFilterStringItem;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Element;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Table;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\TableHeader;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridPaginator;
use Oro\Bundle\TestFrameworkBundle\Behat\Context\OroFeatureContext;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\OroPageObjectAware;
use Oro\Bundle\TestFrameworkBundle\Tests\Behat\Context\OroMainContext;
use Oro\Bundle\TestFrameworkBundle\Tests\Behat\Context\PageObjectDictionary;

use Symfony\Component\DomCrawler\Crawler;

/**
 * @SuppressWarnings(PHPMD.TooManyMethods)
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
 * @SuppressWarnings(PHPMD.ExcessiveClassLength)
 */
class GridContext extends OroFeatureContext implements OroPageObjectAware
{
    use PageObjectDictionary;

    /**
     * @var OroMainContext
     */
    private $oroMainContext;

    /**
     * @BeforeScenario
     */
    public function gatherContexts(BeforeScenarioScope $scope)
    {
        $environment = $scope->getEnvironment();
        $this->oroMainContext = $environment->getContext(OroMainContext::class);
    }

    /**
     * @var int
     */
    protected $gridRecordsNumber;

    /**
     * @When I don't select any record from Grid
     */
    public function iDonTSelectAnyRecordFromGrid()
    {
        // No need to do anything
    }

    /**
     * Mass inline grid field edit
     * Accept table and pass it to inlineEditField
     * Example: When I edit first record from grid:
     *            | name      | editedName       |
     *            | status    | Qualified        |
     *
     * @Then I edit first record from grid:
     * @param TableNode $table
     */
    public function iEditFirstRecordFromGrid(TableNode $table)
    {
        foreach ($table->getRows() as $row) {
            list($field, $value) = $row;
            $this->inlineEditField($field, $value);
            $this->waitForAjax();
        }
    }

    /**
     * Inline grid field edit with click on empty space
     * Example: When I edit Status as "Open"
     * Example: Given I edit Probability as "30"
     *
     * @When /^(?:|I )edit (?P<field>[^"]+) as "(?P<value>.*)" with click on empty space$/
     * @When /^(?:|I )edit "(?P<entityTitle>[^"]+)" (?P<field>.+) as "(?P<value>.*)" with click on empty space$/
     */
    public function inlineEditRecordInGridWithClickOnEmptySpace($field, $value, $entityTitle = null)
    {
        $row = $this->getGridRow($entityTitle);

        $row->setCellValue($field, $value);
        // click any where on the page
        $this->getPage()->find('css', '#container')->click();
        $this->oroMainContext->iShouldSeeFlashMessage('Record has been succesfully updated');
    }

    /**
     * Inline grid field edit by double click
     * Example: When I edit Status as "Open"
     * Example: Given I edit Probability as "30"
     *
     * @When /^(?:|I )edit (?P<field>[^"]+) as "(?P<value>.*)" by double click$/
     * @When /^(?:|I )edit "(?P<entityTitle>[^"]+)" (?P<field>.+) as "(?P<value>.*)" by double click$/
     */
    public function inlineEditRecordInGridByDoubleclick($field, $value, $entityTitle = null)
    {
        $row = $this->getGridRow($entityTitle);

        $row->setCellValueByDoubleClick($field, $value);
    }

    /**
     * @param string $entityTitle
     * @return \Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridRow
     */
    protected function getGridRow($entityTitle = null)
    {
        /** @var Grid $grid */
        $grid = $this->createElement('Grid');

        if (null !== $entityTitle) {
            $row = $grid->getRowByContent($entityTitle);
        } else {
            $rows = $grid->getRows();
            self::assertCount(1, $rows, sprintf('Expect one row in grid but got %s.' .
                PHP_EOL . 'You can specify row content for edit field in specific row.', count($rows)));

            $row = array_shift($rows);
        }

        return $row;
    }

    /**
     * Inline edit field
     * Example: When I edit Status as "Open"
     * Example: Given I edit Probability as "30"
     *
     * @When /^(?:|I )edit (?P<field>[^"]+) as "(?P<value>.*)"$/
     * @When /^(?:|I )edit "(?P<entityTitle>[^"]+)" (?P<field>.+) as "(?P<value>.*)"$/
     */
    public function inlineEditField($field, $value, $entityTitle = null)
    {
        $row = $this->getGridRow($entityTitle);

        $row->setCellValueAndSave($field, $value);
        $this->oroMainContext->iShouldSeeFlashMessage('Inline edits are being saved');
    }

    /**
     * Inline edit field and don't save
     * Example: When I edit Status as "Open" without saving
     * Example: Given I edit Probability as "30" without saving
     *<commit_msg>BB-9797: Fixed flash message (#10700)<commit_after><?php

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

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;

use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridColumnManager;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridToolBarTools;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\MultipleChoice;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\Grid;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridFilterDateTimeItem;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridFilters;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridFilterStringItem;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Element;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\Table;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\TableHeader;
use Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridPaginator;
use Oro\Bundle\TestFrameworkBundle\Behat\Context\OroFeatureContext;
use Oro\Bundle\TestFrameworkBundle\Behat\Element\OroPageObjectAware;
use Oro\Bundle\TestFrameworkBundle\Tests\Behat\Context\OroMainContext;
use Oro\Bundle\TestFrameworkBundle\Tests\Behat\Context\PageObjectDictionary;

use Symfony\Component\DomCrawler\Crawler;

/**
 * @SuppressWarnings(PHPMD.TooManyMethods)
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
 * @SuppressWarnings(PHPMD.ExcessiveClassLength)
 */
class GridContext extends OroFeatureContext implements OroPageObjectAware
{
    use PageObjectDictionary;

    /**
     * @var OroMainContext
     */
    private $oroMainContext;

    /**
     * @BeforeScenario
     */
    public function gatherContexts(BeforeScenarioScope $scope)
    {
        $environment = $scope->getEnvironment();
        $this->oroMainContext = $environment->getContext(OroMainContext::class);
    }

    /**
     * @var int
     */
    protected $gridRecordsNumber;

    /**
     * @When I don't select any record from Grid
     */
    public function iDonTSelectAnyRecordFromGrid()
    {
        // No need to do anything
    }

    /**
     * Mass inline grid field edit
     * Accept table and pass it to inlineEditField
     * Example: When I edit first record from grid:
     *            | name      | editedName       |
     *            | status    | Qualified        |
     *
     * @Then I edit first record from grid:
     * @param TableNode $table
     */
    public function iEditFirstRecordFromGrid(TableNode $table)
    {
        foreach ($table->getRows() as $row) {
            list($field, $value) = $row;
            $this->inlineEditField($field, $value);
            $this->waitForAjax();
        }
    }

    /**
     * Inline grid field edit with click on empty space
     * Example: When I edit Status as "Open"
     * Example: Given I edit Probability as "30"
     *
     * @When /^(?:|I )edit (?P<field>[^"]+) as "(?P<value>.*)" with click on empty space$/
     * @When /^(?:|I )edit "(?P<entityTitle>[^"]+)" (?P<field>.+) as "(?P<value>.*)" with click on empty space$/
     */
    public function inlineEditRecordInGridWithClickOnEmptySpace($field, $value, $entityTitle = null)
    {
        $row = $this->getGridRow($entityTitle);

        $row->setCellValue($field, $value);
        // click any where on the page
        $this->getPage()->find('css', '#container')->click();
        $this->oroMainContext->iShouldSeeFlashMessage('Record has been succesfully updated');
    }

    /**
     * Inline grid field edit by double click
     * Example: When I edit Status as "Open"
     * Example: Given I edit Probability as "30"
     *
     * @When /^(?:|I )edit (?P<field>[^"]+) as "(?P<value>.*)" by double click$/
     * @When /^(?:|I )edit "(?P<entityTitle>[^"]+)" (?P<field>.+) as "(?P<value>.*)" by double click$/
     */
    public function inlineEditRecordInGridByDoubleclick($field, $value, $entityTitle = null)
    {
        $row = $this->getGridRow($entityTitle);

        $row->setCellValueByDoubleClick($field, $value);
    }

    /**
     * @param string $entityTitle
     * @return \Oro\Bundle\DataGridBundle\Tests\Behat\Element\GridRow
     */
    protected function getGridRow($entityTitle = null)
    {
        /** @var Grid $grid */
        $grid = $this->createElement('Grid');

        if (null !== $entityTitle) {
            $row = $grid->getRowByContent($entityTitle);
        } else {
            $rows = $grid->getRows();
            self::assertCount(1, $rows, sprintf('Expect one row in grid but got %s.' .
                PHP_EOL . 'You can specify row content for edit field in specific row.', count($rows)));

            $row = array_shift($rows);
        }

        return $row;
    }

    /**
     * Inline edit field
     * Example: When I edit Status as "Open"
     * Example: Given I edit Probability as "30"
     *
     * @When /^(?:|I )edit (?P<field>[^"]+) as "(?P<value>.*)"$/
     * @When /^(?:|I )edit "(?P<entityTitle>[^"]+)" (?P<field>.+) as "(?P<value>.*)"$/
     */
    public function inlineEditField($field, $value, $entityTitle = null)
    {
        $row = $this->getGridRow($entityTitle);

        $row->setCellValueAndSave($field, $value);
        $this->oroMainContext->iShouldSeeFlashMessage('Record has been succesfully updated');
    }

    /**
     * Inline edit field and don't save
     * Example: When I edit Status as "Open" without saving
     * Example: Given I edit Probability as "30" without saving
     *
{'fraction_non_alphanumeric': 0.14116683353053608, 'fraction_numerical': 0.0024574497132975335, 'mean_word_length': 3.0190197512801755, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 77, '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': '4044277', 'n_tokens_mistral': 3464, 'n_tokens_neox': 3386, 'n_words': 744}