/**
  * Tests adding, getting, and order of priorities.
  *
  * @covers ::addProvider
  * @covers ::getSortedProviders
  * @covers ::getProvider
  * @covers ::isGlobal
  */
 public function testAuthenticationCollector()
 {
     $providers = [];
     $global = [];
     $authentication_collector = new AuthenticationCollector();
     $priorities = [2, 0, -8, 10, 1, 3, -5, 0, 6, -10, -4];
     foreach ($priorities as $priority) {
         $provider_id = $this->randomMachineName();
         $provider = new TestAuthenticationProvider($provider_id);
         $providers[$priority][$provider_id] = $provider;
         $global[$provider_id] = rand(0, 1) > 0.5;
         $authentication_collector->addProvider($provider, $provider_id, $priority, $global[$provider_id]);
     }
     // Sort the $providers array by priority (highest number is lowest priority)
     // and compare with AuthenticationCollector::getSortedProviders().
     krsort($providers);
     // Merge nested providers from $providers into $sorted_providers.
     $sorted_providers = [];
     foreach ($providers as $providers_priority) {
         $sorted_providers = array_merge($sorted_providers, $providers_priority);
     }
     $this->assertEquals($sorted_providers, $authentication_collector->getSortedProviders());
     // Test AuthenticationCollector::getProvider() and
     // AuthenticationCollector::isGlobal().
     foreach ($sorted_providers as $provider) {
         $this->assertEquals($provider, $authentication_collector->getProvider($provider->providerId));
         $this->assertEquals($global[$provider->providerId], $authentication_collector->isGlobal($provider->providerId));
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $io = new DrupalStyle($input, $output);
     $resource_id = $input->getArgument('resource-id');
     $rest_resources = $this->getRestResources();
     $rest_resources_ids = array_merge(array_keys($rest_resources['enabled']), array_keys($rest_resources['disabled']));
     if (!$resource_id) {
         $resource_id = $io->choiceNoList($this->trans('commands.rest.enable.arguments.resource-id'), $rest_resources_ids);
     }
     $this->validateRestResource($resource_id, $rest_resources_ids, $this->getTranslator());
     $input->setArgument('resource-id', $resource_id);
     // Calculate states available by resource and generate the question
     $plugin = $this->pluginManagerRest->getInstance(['id' => $resource_id]);
     $states = $plugin->availableMethods();
     $state = $io->choice($this->trans('commands.rest.enable.arguments.states'), $states);
     $io->writeln($this->trans('commands.rest.enable.messages.selected-state') . ' ' . $state);
     // Get Authentication Provider and generate the question
     $authenticationProviders = $this->authenticationCollector->getSortedProviders();
     $authenticationProvidersSelected = $io->choice($this->trans('commands.rest.enable.messages.authentication-providers'), array_keys($authenticationProviders), 0, true);
     $io->writeln($this->trans('commands.rest.enable.messages.selected-authentication-providers') . ' ' . implode(', ', $authenticationProvidersSelected));
     $rest_settings = $this->getRestDrupalConfig();
     $rest_settings[$resource_id][$state]['supported_formats'] = $formats;
     $rest_settings[$resource_id][$state]['supported_auth'] = $authenticationProvidersSelected;
     $config = $this->configFactory->getEditable('rest.settings');
     $config->set('resources', $rest_settings);
     $config->save();
     return 0;
 }
 /**
  * @covers ::applyFilter
  */
 public function testApplyFilterWithFilterprovider()
 {
     $auth_provider = $this->getMock('Drupal\\Tests\\Core\\Authentication\\TestAuthenticationProviderInterface');
     $auth_provider->expects($this->once())->method('appliesToRoutedRequest')->willReturn(TRUE);
     $authentication_collector = new AuthenticationCollector();
     $authentication_collector->addProvider($auth_provider, 'filtered', 0);
     $authentication_manager = new AuthenticationManager($authentication_collector);
     $request = new Request();
     $this->assertTrue($authentication_manager->appliesToRoutedRequest($request, FALSE));
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $io = new DrupalStyle($input, $output);
     $resource_id = $input->getArgument('resource-id');
     $rest_resources = $this->getRestResources();
     $rest_resources_ids = array_merge(array_keys($rest_resources['enabled']), array_keys($rest_resources['disabled']));
     if (!$resource_id) {
         $resource_id = $io->choiceNoList($this->trans('commands.rest.enable.arguments.resource-id'), $rest_resources_ids);
     }
     $this->validateRestResource($resource_id, $rest_resources_ids, $this->translator);
     $input->setArgument('resource-id', $resource_id);
     // Calculate states available by resource and generate the question.
     $plugin = $this->pluginManagerRest->getInstance(['id' => $resource_id]);
     $methods = $plugin->availableMethods();
     $method = $io->choice($this->trans('commands.rest.enable.arguments.methods'), $methods);
     $io->writeln($this->trans('commands.rest.enable.messages.selected-method') . ' ' . $method);
     $format = $io->choice($this->trans('commands.rest.enable.arguments.formats'), $this->formats);
     $io->writeln($this->trans('commands.rest.enable.messages.selected-format') . ' ' . $format);
     // Get Authentication Provider and generate the question
     $authenticationProviders = $this->authenticationCollector->getSortedProviders();
     $authenticationProvidersSelected = $io->choice($this->trans('commands.rest.enable.messages.authentication-providers'), array_keys($authenticationProviders), 0, true);
     $io->writeln($this->trans('commands.rest.enable.messages.selected-authentication-providers') . ' ' . implode(', ', $authenticationProvidersSelected));
     $format_resource_id = str_replace(':', '.', $resource_id);
     $config = $this->entityManager->getStorage('rest_resource_config')->load($format_resource_id);
     if (!$config) {
         $config = $this->entityManager->getStorage('rest_resource_config')->create(['id' => $format_resource_id, 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, 'configuration' => []]);
     }
     $configuration = $config->get('configuration') ?: [];
     $configuration[$method] = ['supported_formats' => [$format], 'supported_auth' => $authenticationProvidersSelected];
     $config->set('configuration', $configuration);
     $config->save();
     $message = sprintf($this->trans('commands.rest.enable.messages.success'), $resource_id);
     $io->info($message);
     return true;
 }