setVisibilityConfig example

/** * @var \Drupal\block\BlockInterface */
  protected $entity;

  /** * {@inheritdoc} */
  protected function setUpAuthorization($method) {
    switch ($method) {
      case 'GET':
        $this->entity->setVisibilityConfig('user_role', [])->save();
        break;

      case 'POST':
        $this->grantPermissionsToTestedRole(['administer blocks']);
        break;

      case 'PATCH':
        $this->grantPermissionsToTestedRole(['administer blocks']);
        break;
    }
  }

  
$role1 = Role::create(['id' => 'test_role1', 'label' => 'Test role 1']);
    $role1->save();

    $role2 = Role::create(['id' => 'test_role2', 'label' => 'Test role 2']);
    $role2->save();

    $block = Block::create([
      'id' => $this->randomMachineName(),
      'plugin' => 'system_powered_by_block',
    ]);

    $block->setVisibilityConfig('user_role', [
      'roles' => [
        $role1->id() => $role1->id(),
        $role2->id() => $role2->id(),
      ],
    ]);

    $block->save();

    $this->assertEquals([$role1->id() => $role1->id()$role2->id() => $role2->id()]$block->getVisibility()['user_role']['roles']);

    $role1->delete();

    
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUpAuthorization($method) {
    switch ($method) {
      case 'GET':
        $this->entity->setVisibilityConfig('user_role', [])->save();
        break;
    }
  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    $block = Block::create([
      'plugin' => 'llama_block',
      'region' => 'header',
      
$this->assertSession()->pageTextContains('Your search yielded no results');

    // Test a search from the block on a 404 page.     $this->drupalGet('foo');
    $this->assertSession()->statusCodeEquals(404);
    $this->submitForm($terms, 'Search');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Your search yielded no results');

    $visibility = $block->getVisibility();
    $visibility['request_path']['pages'] = 'search';
    $block->setVisibilityConfig('request_path', $visibility['request_path']);

    $this->drupalGet('');
    $this->submitForm($terms, 'Search');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Your search yielded no results');

    // Confirm that the form submits to the default search page.     /** @var \Drupal\search\SearchPageRepositoryInterface $search_page_repository */
    $search_page_repository = \Drupal::service('search.search_page_repository');
    $entity_id = $search_page_repository->getDefaultSearchPage();
    $this->assertEquals(
      
Home | Imprint | This part of the site doesn't use cookies.