<commit_before><?php
namespace Neos\Flow\Tests\Behavior\Features\Bootstrap;

use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Exception;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\ObjectManagement\Exception\UnknownObjectException;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Utility\Environment;
use Neos\Utility\ObjectAccess;
use Neos\Flow\Security;
use Neos\Flow\Security\Authentication\AuthenticationProviderManager;
use Neos\Flow\Security\Authentication\Provider\TestingProvider;
use Neos\Flow\Security\Authentication\TokenInterface;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Flow\Security\Exception\AccessDeniedException;
use Neos\Flow\Security\Policy\PolicyService;
use Neos\Flow\Tests\Functional\Security\Fixtures\Controller\AuthenticationController;
use Neos\Utility\Arrays;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Symfony\Component\Yaml\Yaml;

/**
 * A trait with shared step definitions for testing compile time security privilege types
 * (e.g. method privileges)
 *
 * Note that this trait requires that the following members are available:
 *
 * - $this->objectManager (Neos\Flow\ObjectManagement\ObjectManagerInterface)
 * - $this->environment (Neos\Flow\Utility\Environment)
 *
 * Note: This trait expects the IsolatedBehatStepsTrait to be available!
 *
 * Note: Make sure to call $this->setupSecurity() in the constructor of your
 * behat context for these steps to work in your tests!
 *
 * @property Environment environment
 * @property ObjectManagerInterface objectManager
 */
trait SecurityOperationsTrait
{
    protected $securityInitialized = false;

    protected static $testingPolicyPathAndFilename;

    /**
     * @var Security\AccountRepository
     */
    protected $accountRepository;

    /**
     * @var AuthenticationProviderManager
     */
    protected $authenticationManager;

    /**
     * @var Security\Authentication\TokenAndProviderFactoryInterface
     */
    protected $tokenAndProviderFactory;

    /**
     * @var PolicyService
     */
    protected $policyService;

    /**
     * @var PrivilegeManagerInterface
     */
    protected $privilegeManager;

    /**
     * @var Security\Context
     */
    protected $securityContext;

    /**
     * @var TestingProvider
     */
    protected $testingProvider;

    /**
     * WARNING: If using this step definition, IT MUST RUN AS ABSOLUTELY FIRST STEP IN A SCENARIO!
     *
     * @Given /^I have the following policies:$/
     * @throws \Exception
     */
    public function iHaveTheFollowingPolicies($string)
    {
        if ($this->subProcess !== null) {
            // This check ensures that this statement is ran *before* a subprocess is opened; as the Policy.yaml
            // which is set here influences the Proxy Building Process.
            throw new \Exception('Step "I have the following policies:" must run as FIRST step in a scenario, because otherwise the proxy-classes are already built in the wrong manner!');
        }

        $configurationManager = $this->objectManager->get(ConfigurationManager::class);
        $configurationManager->registerConfigurationType(ConfigurationManager::CONFIGURATION_TYPE_POLICY, static function() use ($string) {
            return Yaml::parse($string);
        });

        $policyService = $this->objectManager->get(PolicyService::class);
        ObjectAccess::setProperty($policyService, 'initialized', false, true);
    }

    /**
     * @AfterFeature
     * @BeforeFeature<commit_msg>set temporary policy configuration for Behat tests<commit_after><?php
namespace Neos\Flow\Tests\Behavior\Features\Bootstrap;

use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Exception;
use Neos\Flow\Mvc\ActionRequest;
use Neos\Flow\ObjectManagement\Exception\UnknownObjectException;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Utility\Environment;
use Neos\Utility\ObjectAccess;
use Neos\Flow\Security;
use Neos\Flow\Security\Authentication\AuthenticationProviderManager;
use Neos\Flow\Security\Authentication\Provider\TestingProvider;
use Neos\Flow\Security\Authentication\TokenInterface;
use Neos\Flow\Security\Authorization\PrivilegeManagerInterface;
use Neos\Flow\Security\Exception\AccessDeniedException;
use Neos\Flow\Security\Policy\PolicyService;
use Neos\Flow\Tests\Functional\Security\Fixtures\Controller\AuthenticationController;
use Neos\Utility\Arrays;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Symfony\Component\Yaml\Yaml;

/**
 * A trait with shared step definitions for testing compile time security privilege types
 * (e.g. method privileges)
 *
 * Note that this trait requires that the following members are available:
 *
 * - $this->objectManager (Neos\Flow\ObjectManagement\ObjectManagerInterface)
 * - $this->environment (Neos\Flow\Utility\Environment)
 *
 * Note: This trait expects the IsolatedBehatStepsTrait to be available!
 *
 * Note: Make sure to call $this->setupSecurity() in the constructor of your
 * behat context for these steps to work in your tests!
 *
 * @property Environment environment
 * @property ObjectManagerInterface objectManager
 */
trait SecurityOperationsTrait
{
    protected $securityInitialized = false;

    protected static $testingPolicyPathAndFilename;

    /**
     * @var Security\AccountRepository
     */
    protected $accountRepository;

    /**
     * @var AuthenticationProviderManager
     */
    protected $authenticationManager;

    /**
     * @var Security\Authentication\TokenAndProviderFactoryInterface
     */
    protected $tokenAndProviderFactory;

    /**
     * @var PolicyService
     */
    protected $policyService;

    /**
     * @var PrivilegeManagerInterface
     */
    protected $privilegeManager;

    /**
     * @var Security\Context
     */
    protected $securityContext;

    /**
     * @var TestingProvider
     */
    protected $testingProvider;

    /**
     * WARNING: If using this step definition, IT MUST RUN AS ABSOLUTELY FIRST STEP IN A SCENARIO!
     *
     * @Given /^I have the following policies:$/
     * @throws \Exception
     */
    public function iHaveTheFollowingPolicies($string)
    {
        if ($this->subProcess !== null) {
            // This check ensures that this statement is ran *before* a subprocess is opened; as the Policy.yaml
            // which is set here influences the Proxy Building Process.
            throw new \Exception('Step "I have the following policies:" must run as FIRST step in a scenario, because otherwise the proxy-classes are already built in the wrong manner!');
        }

        self::$testingPolicyPathAndFilename = $this->environment->getPathToTemporaryDirectory() . 'Policy.yaml';
        file_put_contents(self::$testingPolicyPathAndFilename, $string->getRaw());

        $policyService = $this->objectManager->get(PolicyService::class);
        ObjectAccess::setProperty($policyService, 'initialized', false, true);
    }

    /**
     * @AfterFeature
     * @BeforeFeature
{'fraction_non_alphanumeric': 0.09548022598870057, 'fraction_numerical': 0.0, 'mean_word_length': 3.778002699055331, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '3836535', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1848, 'n_words': 487}