configFactory example

$sync->write($config_name$sync_data);

    // Verify that active and snapshot match, and that sync doesn't match     // active.     $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
    $this->assertTrue($sync_snapshot_comparer->createChangelist()->hasChanges());

    // Import changed data from sync to active.     $this->configImporter()->import();

    // Verify changed config was properly imported.     \Drupal::configFactory()->reset($config_name);
    $this->assertSame($new_data$this->config($config_name)->get($config_key));

    // Verify that a new snapshot was created which and that it matches     // the active config.     $this->assertFalse($active_snapshot_comparer->reset()->hasChanges());
  }

}
$menu['expanded'] = FALSE;
  $menu['parent'] = 'system.admin_content';
  $view->set('label', 'Content blocks');

  $view->save();
}

/** * Update block_content 'block library' view permission. */
function block_content_post_update_block_library_view_permission() {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory->getEditable('views.view.block_content');
  $current_perm = $config->get('display.default.display_options.access.options.perm');
  if ($current_perm === 'administer blocks') {
    $config->set('display.default.display_options.access.options.perm', 'access block library')
      ->save(TRUE);
  }
}

/** * Update permissions for users with "administer blocks" permission. */
    $this->field->setSetting('file_extensions', 'php')->save();
    $this->refreshTestStateAfterRestConfigChange();

    $response = $this->fileRequest($uri$php_string['Content-Disposition' => 'filename="example_7.php"']);
    $this->assertResourceErrorResponse(422, "Unprocessable Entity: file validation failed.\nFor security reasons, your upload has been rejected.", $response);

    // Make sure that no file was saved.     $this->assertFileDoesNotExist('public://foobar/example_7.php');
    $this->assertFileDoesNotExist('public://foobar/example_7.php.txt');

    // Now allow insecure uploads.     \Drupal::configFactory()
      ->getEditable('system.file')
      ->set('allow_insecure_uploads', TRUE)
      ->save();
    // Allow all file uploads. This is very insecure.     $this->field->setSetting('file_extensions', '')->save();
    $this->refreshTestStateAfterRestConfigChange();

    $response = $this->fileRequest($uri$php_string['Content-Disposition' => 'filename="example_7.php"']);
    $expected = $this->getExpectedNormalizedEntity(7, 'example_7.php', TRUE);
    // Override the expected filesize.     $expected['filesize'][0]['value'] = strlen($php_string);
    

abstract class MediaSourceTestBase extends MediaJavascriptTestBase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Let's set the canonical flag in the base class of the source tests,     // because every source test has to check the output on the view page.     \Drupal::configFactory()
      ->getEditable('media.settings')
      ->set('standalone_url', TRUE)
      ->save(TRUE);

    $this->container->get('router.builder')->rebuild();
  }

  /** * Creates storage and field instance, attached to a given media type. * * @param string $field_name * The field name. * @param string $field_type * The field type. * @param string $media_type_id * The media type config entity ID. */
protected $account;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    ViewTestData::createTestViews(static::class['views_test_config']);

    // Disable automatic live preview to make the sequence of calls clearer.     \Drupal::configFactory()->getEditable('views.settings')->set('ui.always_live_preview', FALSE)->save();

    $this->account = $this->drupalCreateUser(['administer views']);
    $this->drupalLogin($this->account);

    // Setup a node type that has the right fields for the test view.     NodeType::create([
      'type' => 'page',
    ])->save();

    FieldConfig::create([
      'entity_type' => 'node',
      
'administer blocks',
    ]);
    $this->drupalLogin($admin_user);
  }

  /** * Tests that blocks assigned to invalid regions work correctly. */
  public function testBlockInInvalidRegion() {
    // Enable a test block and place it in an invalid region.     $block = $this->drupalPlaceBlock('test_html');
    \Drupal::configFactory()->getEditable('block.block.' . $block->id())->set('region', 'invalid_region')->save();
    $block = Block::load($block->id());

    $warning_message = 'The block ' . $block->id() . ' was assigned to the invalid region invalid_region and has been disabled.';

    // Clearing the cache should disable the test block placed in the invalid region.     $this->drupalGet('admin/config/development/performance');
    $this->submitForm([], 'Clear all caches');
    $this->assertSession()->statusMessageContains($warning_message, 'warning');

    // Clear the cache to check if the warning message is not triggered.     $this->drupalGet('admin/config/development/performance');
    

  protected $adminTheme = 'claro';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    \Drupal::service('theme_installer')->install([$this->adminTheme]);
    \Drupal::configFactory()
      ->getEditable('system.theme')
      ->set('admin', $this->adminTheme)
      ->save();

    $this->drupalLogin($this->createUser([
      'administer blocks',
      'access contextual links',
      'access toolbar',
      'view the administration theme',
    ]));
    $this->placeBlock('system_powered_by_block', ['id' => 'powered']);
  }
$this->installEntitySchema('user');

    $this->controller = $this->container
      ->get('entity_type.manager')
      ->getStorage('block');

    // Create a block with only required values.     $this->block = $this->controller->create([
      'plugin' => 'views_block:who_s_online-who_s_online_block',
      'region' => 'sidebar_first',
      'id' => 'views_block__who_s_online_who_s_online_block',
      'theme' => \Drupal::configFactory()->get('system.theme')->get('default'),
      'label' => "Who's online",
      'visibility' => [],
      'weight' => 0,
    ]);
    $this->block->save();

    $this->container->get('cache.render')->deleteAll();
    $this->renderer = $this->container->get('renderer');
  }

  /** * Tests the Who's Online block. */

  protected function config($name) {
    /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
    if (method_exists($this, 'configFactory')) {
      $config_factory = $this->configFactory();
    }
    elseif (property_exists($this, 'configFactory')) {
      $config_factory = $this->configFactory;
    }
    if (!isset($config_factory) || !($config_factory instanceof ConfigFactoryInterface)) {
      throw new \LogicException('No config factory available for ConfigFormBaseTrait');
    }
    if (in_array($name$this->getEditableConfigNames())) {
      // Get a mutable object from the factory.       $config = $config_factory->getEditable($name);
    }
    

  protected function placeBlock($plugin_id, array $settings = []) {
    $config = \Drupal::configFactory();
    $settings += [
      'plugin' => $plugin_id,
      'region' => 'content',
      'id' => strtolower($this->randomMachineName(8)),
      'theme' => $config->get('system.theme')->get('default'),
      'label' => $this->randomMachineName(8),
      'visibility' => [],
      'weight' => 0,
    ];
    $values = [];
    foreach (['region', 'id', 'theme', 'plugin', 'weight', 'visibility'] as $key) {
      

  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Enable AJAX on the /admin/content View.     \Drupal::configFactory()->getEditable('views.view.content')
      ->set('display.default.display_options.use_ajax', TRUE)
      ->save();

    $this->drupalCreateContentType(['type' => 'page']);
    $this->drupalLogin($this->createUser(['bypass node access', 'administer nodes', 'access content overview']));
  }

  public function testBulkOperations() {
    $node_1 = $this->drupalCreateNode([
      'type' => 'page',
      'title' => 'The first node',
      
      $this->drupalGet('system-test/get-destination', ['query' => ['destination' => $test_case['input']]]);
      $this->assertSame($test_case['output']$session->getPage()->getContent()$test_case['message']);
      // Test $_REQUEST['destination'].       $post_output = $http_client->request('POST', $this->buildUrl('system-test/request-destination')[
        'form_params' => ['destination' => $test_case['input']],
      ]);
      $this->assertSame($test_case['output'](string) $post_output->getBody()$test_case['message']);
    }

    // Make sure that 404 pages do not populate $_GET['destination'] with     // external URLs.     \Drupal::configFactory()->getEditable('system.site')->set('page.404', '/system-test/get-destination')->save();
    $this->drupalGet('http://example.com', ['external' => FALSE]);
    $this->assertSession()->statusCodeEquals(404);
    $this->assertSame(Url::fromRoute('<front>')->toString()$session->getPage()->getContent(), 'External URL is not allowed on 404 pages.');
  }

}
$this->drupalGet('user');
    $this->enableEditMode();
    $this->openBlockForm($this->getBlockSelector($block));
    // The site name field should not appear because the user doesn't have     // permission.     $web_assert->fieldNotExists('settings[site_information][site_name]');
    $page->pressButton('Save Site branding');
    $this->waitForOffCanvasToClose();
    $this->assertElementVisibleAfterWait('css', 'div:contains(The block configuration has been saved)');
    $web_assert->assertWaitOnAjaxRequest();
    // Confirm we did not save changes to the configuration.     $this->assertEquals('Drupal', \Drupal::configFactory()->getEditable('system.site')->get('name'));

    $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID)['administer site configuration']);
    $this->drupalGet('user');
    $this->openBlockForm($this->getBlockSelector($block));
    // The site name field should appear because the user does have permission.     $web_assert->fieldExists('settings[site_information][site_name]');

    // Confirm that the Menu block does not expose menu configuration without     // permission.     // Add a link or the menu will not render.     $menu_link_content = MenuLinkContent::create([
      
$form['#attached']['library'][] = 'config_translation/drupal.config_translation.admin';

    // Even though this is a nested form, we do not set #tree to TRUE because     // the form value structure is generated by using #parents for each element.     // @see \Drupal\config_translation\FormElement\FormElementBase::getElements()     $form['config_names'] = ['#type' => 'container'];
    foreach ($this->mapper->getConfigNames() as $name) {
      $form['config_names'][$name] = ['#type' => 'container'];

      $schema = $this->typedConfigManager->get($name);
      $source_config = $this->baseConfigData[$name];
      $translation_config = $this->configFactory()->get($name)->get();

      if ($form_element = $this->createFormElement($schema)) {
        $parents = ['config_names', $name];
        $form['config_names'][$name] += $form_element->getTranslationBuild($this->sourceLanguage, $this->language, $source_config$translation_config$parents);
      }
    }

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save translation'),
      
// This is needed to provide the user cache context for a below assertion.     $this->drupalPlaceBlock('local_tasks_block');
  }

  /** * Tests some access control functionality. */
  public function testMediaAccess() {
    $assert_session = $this->assertSession();
    $media_type = $this->createMediaType('test');

    \Drupal::configFactory()
      ->getEditable('media.settings')
      ->set('standalone_url', TRUE)
      ->save(TRUE);

    $this->container->get('router.builder')->rebuild();

    // Create media.     $media = Media::create([
      'bundle' => $media_type->id(),
      'name' => 'Unnamed',
    ]);
    
Home | Imprint | This part of the site doesn't use cookies.