cssSelect example

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

  /** * Tests the Views form wrapper. */
  public function testFormWrapper() {
    $this->drupalGet('test_bulk_form');
    // Ensure we have the form tag on the page.     $xpath = $this->cssSelect('.views-form form');
    $this->assertCount(1, $xpath, 'There is one views form on the page.');
    // Ensure we don't have nested form elements.     $result = (bool) preg_match('#<form[^>]*?>(?!/form).*<form#s', $this->getSession()->getPage()->getContent());
    $this->assertFalse($result, 'The views form element is not nested.');

    // Test the form isn't shown when the display doesn't use fields.     $this->drupalGet('display-without-fields');
    // Ensure there's no form.     $xpath = $this->cssSelect('.views-form form');
    $this->assertCount(0, $xpath);
  }

}


  /** * Ensures that the entity title is displayed. */
  public function testLayoutPageTitle() {
    $this->drupalPlaceBlock('page_title_block');
    $node = $this->createSectionNode([]);

    $this->drupalGet($node->toUrl('canonical')->toString() . '/layout');
    $this->assertSession()->titleEquals('Edit layout for The node title | Drupal');
    $this->assertEquals('Edit layout for The node title', $this->cssSelect('h1.page-title')[0]->getText());
  }

  /** * Tests that no Layout link shows without a section field. */
  public function testLayoutUrlNoSectionField() {
    $node = $this->createNode([
      'type' => 'bundle_without_section_field',
      'title' => 'The node title',
      'body' => [
        [
          

  protected function assertFieldAttributes(EntityInterface $entity, array $attributes) {
    $view_builder = $this->container->get('entity_type.manager')->getViewBuilder($entity->getEntityTypeId());
    $build = $view_builder->view($entity);
    $this->render($build);

    $actual = array_map(function D\SimpleXMLElement $element) {
      return (string) $element->attributes();
    }$this->cssSelect('.field'));
    $this->assertSame($attributes$actual);
  }

}
/** * Tests that region attributes can be manipulated via preprocess functions. */
  public function testRegionClass() {
    \Drupal::service('module_installer')->install(['block', 'theme_region_test']);

    // Place a block.     $this->drupalPlaceBlock('system_main_block', [
      'region' => 'sidebar_first',
    ]);
    $this->drupalGet('');
    $elements = $this->cssSelect(".region-sidebar-first.new_class");
    $this->assertCount(1, $elements, 'New class found.');
  }

  /** * Ensures suggestion preprocess functions run for default implementations. * * The theme hook used by this test has its base preprocess function in a * separate file, so this test also ensures that the file is correctly loaded * when needed. */
  public function testSuggestionPreprocessForDefaults() {
    
// Enable user pictures on nodes.     $this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();

    $image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style');
    $style = ImageStyle::load($image_style_id);
    $image_url = \Drupal::service('file_url_generator')->transformRelative($style->buildUrl($file->getFileUri()));
    $alt_text = 'Profile picture for user ' . $this->webUser->getAccountName();

    // Verify that the image is displayed on the node page.     $this->drupalGet('node/' . $node->id());
    $elements = $this->cssSelect('article > footer img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
    $this->assertCount(1, $elements, 'User picture with alt text found on node page.');

    // Enable user pictures on comments, instead of nodes.     $this->config('system.theme.global')
      ->set('features.node_user_picture', FALSE)
      ->set('features.comment_user_picture', TRUE)
      ->save();

    $edit = [
      'comment_body[0][value]' => $this->randomString(),
    ];
    
$this->drupalGet('admin/modules');
    $this->assertSession()->elementExists('css', '#block-claro-help');
  }

  /** * Tests that the Claro theme can be uninstalled. */
  public function testIsUninstallable() {
    $this->drupalLogin($this->drupalCreateUser(['access administration pages', 'administer themes']));

    $this->drupalGet('admin/appearance');
    $this->cssSelect('a[title="Install <strong>Test theme</strong> as default theme"]')[0]->click();
    $this->cssSelect('a[title="Uninstall Claro theme"]')[0]->click();
    $this->assertSession()->pageTextContains('The Claro theme has been uninstalled.');
  }

  /** * Tests pager attribute is present using pager_test. */
  public function testPagerAttribute(): void {
    // Insert 300 log messages.     $logger = $this->container->get('logger.factory')->get('pager_test');
    for ($i = 0; $i < 300; $i++) {
      

  public static $testViews = ['test_new_comments', 'test_comment_count'];

  /** * Tests the new comments field plugin. */
  public function testNewComments() {
    $this->drupalGet('test-new-comments');
    $this->assertSession()->statusCodeEquals(200);
    $new_comments = $this->cssSelect(".views-field-new-comments a:contains('1')");
    $this->assertCount(1, $new_comments, 'Found the number of new comments for a certain node.');
  }

  /** * Test the comment count field. */
  public function testCommentCount() {
    $this->drupalGet('test-comment-count');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertCount(2, $this->cssSelect('.views-row'));
    $comment_count_with_comment = $this->cssSelect(".views-field-comment-count span:contains('1')");
    
    // subtree hash are different.     $this->assertNotEmpty($new_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
    $this->assertNotEquals($this->hash, $new_subtree_hash, 'The user-specific subtree menu hash has been updated.');
  }

  /** * Tests that back to site link exists on admin pages, not on content pages. */
  public function testBackToSiteLink() {
    // Back to site link should exist in the markup.     $this->drupalGet('test-page');
    $back_link = $this->cssSelect('.home-toolbar-tab');
    $this->assertNotEmpty($back_link);
  }

  /** * Tests that external links added to the menu appear in the toolbar. */
  public function testExternalLink() {
    $edit = [
      'title[0][value]' => 'External URL',
      'link[0][uri]' => 'http://example.org',
      'menu_parent' => 'admin:system.admin',
      
    foreach (range(1, 51) as $delta) {
      Node::create([
        'type' => 'moderated_content',
        'uid' => $this->adminUser->id(),
        'title' => 'Node ' . $delta,
        'status' => 1,
        'moderation_state' => 'published',
      ])->save();
    }
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('admin/content');
    $element = $this->cssSelect('nav.pager li.is-active a');
    $url = $element[0]->getAttribute('href');
    $query = [];
    parse_str(parse_url($url, PHP_URL_QUERY)$query);
    $this->assertEquals(0, $query['page']);
  }

  /** * Tests the workflow when a user has no Content Moderation permissions. */
  public function testNoContentModerationPermissions() {
    $session_assert = $this->assertSession();

    
/** * Tests installing experimental themes and dependencies in the UI. */
  public function testExperimentalConfirmForm(): void {
    // Only experimental themes should be marked as such with a parenthetical.     $this->drupalGet('admin/appearance');
    $this->assertSession()->responseContains(sprintf('Experimental test %s (experimental theme)', \Drupal::VERSION));
    $this->assertSession()->responseContains(sprintf('Experimental dependency test %s', \Drupal::VERSION));

    // First, test installing a non-experimental theme with no dependencies.     // There should be no confirmation form and no experimental theme warning.     $this->cssSelect('a[title="Install <strong>Test theme</strong> theme"]')[0]->click();
    $this->assertSession()->pageTextContains('The <strong>Test theme</strong> theme has been installed.');
    $this->assertSession()->pageTextNotContains('Experimental modules are provided for testing purposes only.');

    // Next, test installing an experimental theme with no dependencies.     // There should be a confirmation form with an experimental warning, but no     // list of dependencies.     $this->drupalGet('admin/appearance');
    $this->cssSelect('a[title="Install Experimental test theme"]')[0]->click();
    $this->assertSession()->pageTextContains('Experimental themes are provided for testing purposes only. Use at your own risk.');

    // The module should not be enabled and there should be a warning and a
// The entity fields for name and mail have no meaning if the user is not     // Anonymous.     $this->assertNull($comment1->name->value);
    $this->assertNull($comment1->mail->value);

    // Confirm that the comment was created.     $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
    // Tests that markup is created for comment with heading.     $this->assertSession()->responseMatches('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|');
    // Tests that the comment's title link is the permalink of the comment.     $comment_permalink = $this->cssSelect('.permalink');
    $comment_permalink = $comment_permalink[0]->getAttribute('href');
    // Tests that the comment's title link contains the URL fragment.     $this->assertStringContainsString('#comment-' . $comment1->id()$comment_permalink, "The comment's title link contains the url fragment.");
    $this->assertEquals($comment1->permalink()->toString()$comment_permalink, "The comment's title has the correct link.");
  }

}
/** * Tests that the Olivero theme can be uninstalled. */
  public function testIsUninstallable() {
    $this->drupalLogin($this->drupalCreateUser([
      'access administration pages',
      'administer themes',
    ]));

    $this->drupalGet('admin/appearance');
    $this->cssSelect('a[title="Install <strong>Test theme</strong> as default theme"]')[0]->click();
    $this->cssSelect('a[title="Uninstall Olivero theme"]')[0]->click();
    $this->assertSession()->pageTextContains('The Olivero theme has been uninstalled.');
  }

  /** * Tests pager attribute is present using pager_test. */
  public function testPagerAttribute(): void {
    // Insert 300 log messages.     $logger = \Drupal::logger('pager_test');
    for ($i = 0; $i < 300; $i++) {
      

    $node->save();
    $this->nodes[] = $node;
  }

  /** * Tests rendering caching of a views block with arguments. */
  public function testEmptyView() {
    $this->placeBlock('views_block:node_id_argument-block_1', ['region' => 'header']);
    $this->drupalGet('<front>');
    $this->assertEquals([]$this->cssSelect('div.region-header div.views-field-title'));

    $this->drupalGet($this->nodes[0]->toUrl());
    $result = $this->cssSelect('div.region-header div.views-field-title')[0]->getText();
    $this->assertEquals('test title 1', $result);

    $this->drupalGet($this->nodes[1]->toUrl());
    $result = $this->cssSelect('div.region-header div.views-field-title')[0]->getText();
    $this->assertEquals('test title 2', $result);

    $this->drupalGet($this->nodes[0]->toUrl());
    $result = $this->cssSelect('div.region-header div.views-field-title')[0]->getText();
    
/** * Ensures media entities are rendered correctly. * * @dataProvider providerTestBasics */
  public function testBasics(array $embed_attributes$expected_view_mode, array $expected_attributes, CacheableMetadata $expected_cacheability) {
    $content = $this->createEmbedCode($embed_attributes);

    $result = $this->applyFilter($content);

    $this->assertCount(1, $this->cssSelect('div[data-media-embed-test-view-mode="' . $expected_view_mode . '"]'));
    $this->assertHasAttributes($this->cssSelect('div[data-media-embed-test-view-mode="' . $expected_view_mode . '"]')[0]$expected_attributes);
    $this->assertEqualsCanonicalizing($expected_cacheability->getCacheTags()$result->getCacheTags());
    $this->assertEqualsCanonicalizing($expected_cacheability->getCacheContexts()$result->getCacheContexts());
    $this->assertSame($expected_cacheability->getCacheMaxAge()$result->getCacheMaxAge());
    $this->assertSame(['library']array_keys($result->getAttachments()));
    $this->assertSame(['media/filter.caption']$result->getAttachments()['library']);
  }

  /** * Data provider for testBasics(). */
  
'directory' => 'inline-images',
        'max_size' => '',
        'max_dimensions' => [
          'width' => 0,
          'height' => 0,
        ],
      ],
    ]);
    $editor->save();
    $this->resetAll();
    $this->drupalGet($url);
    $this->assertNotEmpty($this->cssSelect('input[type=text][name="attributes[src]"]'), 'Image uploads disabled: input[type=text][name="attributes[src]"] is present.');
    $this->assertEmpty($this->cssSelect('input[type=file]'), 'Image uploads disabled: input[type=file] is absent.');
    $session->statusCodeEquals(200);

    // With image upload settings, expect a 200, and now there should be an     // input[type=file].     $editor->setImageUploadSettings(['status' => TRUE] + $editor->getImageUploadSettings())
      ->save();
    $this->resetAll();
    $this->drupalGet($url);
    $this->assertEmpty($this->cssSelect('input[type=text][name="attributes[src]"]'), 'Image uploads enabled: input[type=text][name="attributes[src]"] is absent.');
    $this->assertNotEmpty($this->cssSelect('input[type=file]'), 'Image uploads enabled: input[type=file] is present.');
    
Home | Imprint | This part of the site doesn't use cookies.