assertText example

->save();
    $new_expected_fields = [
      'field field--name-name field--type-string field--label-hidden field__item',
      'field field--name-test-field-display-configurable field--type-boolean field--label-above',
      'clearfix text-formatted field field--name-test-display-configurable field--type-text field--label-above',
      'clearfix text-formatted field field--name-test-field-display-post-install field--type-text field--label-above',
      'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
      'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
    ];
    $this->assertFieldAttributes($this->entity, $new_expected_fields);
    $this->assertNotEmpty($this->cssSelect('.layout--onecol'));
    $this->assertText('Test string');
  }

}
$entity_test = EntityTest::create([
        'user_id' => $user->id(),
        'name' => 'test entity test',
      ]);
      $entity_test->save();
    }

    $view = Views::getView('test_entity_row');
    $build = $view->preview();
    $this->render($build);

    $this->assertText('test entity test');
    $this->assertNoText('Member for');

    // Change the view to use a relationship to render the row.     $view = Views::getView('test_entity_row');
    $display = &$view->storage->getDisplay('default');
    $display['display_options']['row']['type'] = 'entity:user';
    $display['display_options']['row']['options']['relationship'] = 'user_id';
    $view->setDisplay('default');
    $build = $view->preview();
    $this->render($build);

    
/** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
    $account_switcher = \Drupal::service('account_switcher');

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    $account_switcher->switchTo($this->userWithAccess);
    $executable = Views::getView($view_id);
    $build = $executable->preview();
    $this->setRawContent($renderer->renderRoot($build));

    $this->assertText($field_content);
    $this->assertTrue(isset($executable->field[$field_name]));

    $account_switcher->switchTo($this->userWithoutAccess);
    $executable = Views::getView($view_id);
    $build = $executable->preview();
    $this->setRawContent($renderer->renderRoot($build));

    $this->assertNoText($field_content);
    $this->assertFalse(isset($executable->field[$field_name]));

    \Drupal::state()->delete('views_field_access_test-field');
  }
$environment = \Drupal::service('twig');
    $node = Node::create([
      'type' => 'page',
      'title' => 'Some node mmk',
      'status' => 1,
      'field_term' => $this->term->id(),
    ]);
    $node->save();
    $template = $environment->load($this->getThemePath('test_theme') . '/templates/node.html.twig');
    $markup = $template->render(['node' => $node]);
    $this->setRawContent($markup);
    $this->assertText('Sometimes people are just jerks');
  }

}
    $this->assertTwigTemplate($this->twig->load('@olivero/layout/page.html.twig'), 'Found page.html.twig in Olivero theme.');
  }

  /** * Tests template extension and includes using namespaces. */
  public function testTwigNamespaces() {
    // Test twig @extends and @include in template files.     $test = ['#theme' => 'twig_namespace_test'];
    $this->setRawContent(\Drupal::service('renderer')->renderRoot($test));

    $this->assertText('This line is from twig_namespace_a/templates/test.html.twig');
    $this->assertText('This line is from twig_namespace_b/templates/test.html.twig');
  }

}
/** * Tests the results area handler. */
  public function testResult() {
    $view = Views::getView('test_area_result');
    $view->setDisplay('default');
    $this->executeView($view);
    $output = $view->render();
    $output = \Drupal::service('renderer')->renderRoot($output);
    $this->setRawContent($output);
    $this->assertText('start: 1 | end: 5 | total: 5 | label: test_area_result | per page: 0 | current page: 1 | current record count: 5 | page count: 1');

    // Make sure that potentially dangerous content was stripped.     $this->assertNoRaw('<script />');
  }

  /** * Tests the results area handler. */
  public function testResultEmpty() {
    $view = Views::getView('test_area_result');

    
->getViewDisplay('node', $node->getType(), 'teaser');
    $display_options = $display->getComponent('body');
    $display_options['settings']['trim_length'] = 200;
    $display->setComponent('body', $display_options)
      ->save();

    // Render the node as a teaser again and check that the summary is now only     // 200 characters in length and so does not include 'What is a Drupalism?'.     $content = $this->drupalBuildEntityView($node, 'teaser');
    $this->assertLessThan(200, strlen($content['body'][0]['#markup']));
    $this->setRawContent($renderer->renderRoot($content));
    $this->assertText($node->label());
    $this->assertNoRaw($expected);
  }

}

  public function testMessageText() {
    \Drupal::messenger()->addStatus('My drupal set message.');

    $view = Views::getView('test_area_messages');

    $view->setDisplay('default');
    $this->executeView($view);
    $output = $view->render();
    $output = \Drupal::service('renderer')->renderRoot($output);
    $this->setRawContent($output);
    $this->assertText('My drupal set message.');
  }

}
// Create the user to test the user fields.     $user = User::create([
      'name' => 'foobar',
      'mail' => 'foobar@example.com',
    ]);
    $build = \Drupal::entityTypeManager()
      ->getViewBuilder('user')
      ->view($user);
    $output = \Drupal::service('renderer')->renderRoot($build);
    $this->setRawContent($output);
    $userEmail = $user->getEmail();
    $this->assertText($userEmail, "User's mail field is found in the twig template");
  }

}
$executable = Views::getView('comment');
    $build = $executable->preview($display_id);
    $this->setRawContent($renderer->renderRoot($build));

    // Assert the exposed filters on the admin page.     $this->assertField('subject');
    $this->assertField('author_name');
    $this->assertField('langcode');

    $elements = $this->cssSelect('input[type="checkbox"]');
    $this->assertCount(2, $elements, 'There are two comments on the page.');
    $this->assertText($comment->label());
    $this->assertText($comment_anonymous->label());
    $executable->destroy();

    // Test the Subject filter.     $executable->setExposedInput(['subject' => 'Anonymous']);
    $build = $executable->preview($display_id);
    $this->setRawContent($renderer->renderRoot($build));

    $elements = $this->cssSelect('input[type="checkbox"]');
    $this->assertCount(1, $elements, 'Only anonymous comment is visible.');
    $this->assertNoText($comment->label());
    
// Set example header text.     $view->header['test_example']->options['string'] = '<p>' . $empty_header . '</p>';
    // Set example footer text.     $view->footer['test_example']->options['string'] = '<p>' . $empty_footer . '</p>';

    // Verify that the empty header and footer sections have not been rendered.     $view->setDisplay('default');
    $this->executeView($view);
    $output = $view->render();
    $output = \Drupal::service('renderer')->renderRoot($output);
    $this->setRawContent($output);
    $this->assertText($empty_text);
    $this->assertNoText($empty_header);
    $this->assertNoText($empty_footer);

    // Enable displaying the header and footer when the View is empty.     $view->header['test_example']->options['empty'] = TRUE;
    $view->footer['test_example']->options['empty'] = TRUE;

    // Verify that the header and footer sections have been rendered.     $this->executeView($view);
    $output = $view->render();
    $output = \Drupal::service('renderer')->renderRoot($output);
    
// Test block output.     \Drupal::currentUser()->setAccount($user1);

    // Test the rendering of a block.     $entity = Block::load('views_block__who_s_online_who_s_online_block');
    $output = \Drupal::entityTypeManager()
      ->getViewBuilder($entity->getEntityTypeId())
      ->view($entity, 'block');
    $this->setRawContent($this->renderer->renderRoot($output));
    $this->assertRaw('2 users', 'Correct number of online users (2 users).');
    $this->assertText($user1->getAccountName(), 'Active user 1 found in online list.');
    $this->assertText($user2->getAccountName(), 'Active user 2 found in online list.');
    $this->assertNoText($user3->getAccountName(), 'Inactive user not found in online list.');
    // Verify that online users are ordered correctly.     $this->assertGreaterThan(strpos($this->getRawContent()$user2->getAccountName())strpos($this->getRawContent()$user1->getAccountName()));
  }

}
$storage->resetCache();
    $entities = $storage->loadMultiple($entity_ids);

    $build = $view_builder->viewMultiple($entities);

    $output = $renderer->renderRoot($build);
    $this->setRawContent($output->__toString());
    // Confirm that the content added in     // entity_test_entity_display_build_alter() appears multiple times, not     // just for the final entity.     foreach ($entity_ids as $id) {
      $this->assertText('Content added in hook_entity_display_build_alter for entity id ' . $id);
    }
  }

}
public function testFieldItemListView() {
    $items = $this->entity->get($this->fieldName);

    \Drupal::service('theme_installer')->install(['stark']);
    $this->config('system.theme')->set('default', 'stark')->save();

    // No display settings: check that default display settings are used.     $build = $items->view();
    $this->render($build);
    $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_default');
    $setting = $settings['test_formatter_setting'];
    $this->assertText($this->label, 'Label was displayed.');
    foreach ($this->values as $delta => $value) {
      $this->assertText($setting . '|' . $value['value']new FormattableMarkup('Value @delta was displayed with expected setting.', ['@delta' => $delta]));
    }

    // Display settings: Check hidden field.     $display = [
      'label' => 'hidden',
      'type' => 'field_test_multiple',
      'settings' => [
        'test_formatter_setting_multiple' => $this->randomMachineName(),
        'alter' => TRUE,
      ],
Home | Imprint | This part of the site doesn't use cookies.