You are here

public function ConfigEntityTrackingTest::testBlockFieldViewsTracking in Entity Usage 8.4

Same name and namespace in other branches
  1. 8.2 tests/src/FunctionalJavascript/ConfigEntityTrackingTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\ConfigEntityTrackingTest::testBlockFieldViewsTracking()
  2. 8.3 tests/src/FunctionalJavascript/ConfigEntityTrackingTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\ConfigEntityTrackingTest::testBlockFieldViewsTracking()

Tests block_field / views tracking.

File

tests/src/FunctionalJavascript/ConfigEntityTrackingTest.php, line 158

Class

ConfigEntityTrackingTest
Tests tracking of config entities.

Namespace

Drupal\Tests\entity_usage\FunctionalJavascript

Code

public function testBlockFieldViewsTracking() {

  // Create block field on the node type.
  $storage = FieldStorageConfig::create([
    'field_name' => 'field_eu_test_related_views',
    'entity_type' => 'node',
    'type' => 'block_field',
  ]);
  $storage
    ->save();
  FieldConfig::create([
    'bundle' => 'eu_test_ct',
    'entity_type' => 'node',
    'field_name' => 'field_eu_test_related_views',
    'label' => 'Related Views',
  ])
    ->save();

  // Define our widget and formatter for this field.
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('node', 'eu_test_ct', 'default')
    ->setComponent('field_eu_test_related_views', [
    'type' => 'block_field_default',
  ])
    ->save();
  \Drupal::service('entity_display.repository')
    ->getViewDisplay('node', 'eu_test_ct', 'default')
    ->setComponent('field_eu_test_related_views', [
    'type' => 'block_field',
  ])
    ->save();
  $this
    ->drupalPlaceBlock('local_tasks_block');
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Check some config-entity related settings on the config form.
  $this
    ->drupalGet('/admin/config/entity-usage/settings');

  // We should have an unchecked checkbox for target entity type.
  $targets_fieldset_wrapper = $assert_session
    ->elementExists('css', '#edit-track-enabled-target-entity-types summary');
  $targets_fieldset_wrapper
    ->click();
  $view_target_checkbox = $assert_session
    ->fieldExists('track_enabled_target_entity_types[entity_types][view]');
  $assert_session
    ->checkboxNotChecked('track_enabled_target_entity_types[entity_types][view]');

  // Check tracking views as targets.
  $view_target_checkbox
    ->click();

  // Also allow views to have the usage tab visible.
  $views_tab_checkbox = $assert_session
    ->fieldExists('local_task_enabled_entity_types[entity_types][view]');
  $views_tab_checkbox
    ->click();

  // Save configuration.
  $page
    ->pressButton('Save configuration');
  $this
    ->saveHtmlOutput();
  $assert_session
    ->pageTextContains('The configuration options have been saved.');

  // Make sure our target view exists.
  $view_name = 'content_recent';
  $view = \Drupal::entityTypeManager()
    ->getStorage('view')
    ->load($view_name);
  $this
    ->assertNotNull($view);

  // Create a node referencing this view through a Block Field field.
  $this
    ->drupalGet('/node/add/eu_test_ct');
  $page
    ->fillField('title[0][value]', 'Node that points to a block with a view');
  $assert_session
    ->optionExists('field_eu_test_related_views[0][plugin_id]', "views_block:{$view_name}-block_1");
  $page
    ->selectFieldOption('field_eu_test_related_views[0][plugin_id]', "views_block:{$view_name}-block_1");
  $assert_session
    ->assertWaitOnAjaxRequest();
  $this
    ->saveHtmlOutput();
  $page
    ->pressButton('Save');
  $this
    ->saveHtmlOutput();
  $this
    ->assertSession()
    ->pageTextContains('eu_test_ct Node that points to a block with a view has been created.');

  /** @var \Drupal\node\NodeInterface $host_node */
  $host_node = $this
    ->getLastEntityOfType('node', TRUE);

  // Check that usage for this view is correctly tracked.
  $usage = \Drupal::service('entity_usage.usage')
    ->listSources($view);
  $expected = [
    'node' => [
      $host_node
        ->id() => [
        [
          'source_langcode' => $host_node
            ->language()
            ->getId(),
          'source_vid' => $host_node
            ->getRevisionId(),
          'method' => 'block_field',
          'field_name' => 'field_eu_test_related_views',
          'count' => 1,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected, $usage);

  // We should also be able to see the usage tab and usage page.
  $this
    ->drupalGet('/admin/structure/views/view/content_recent');
  $assert_session
    ->linkExists('Usage');
  $this
    ->drupalGet('/admin/structure/views/view/content_recent/usage');
  $first_row_title_link = $assert_session
    ->elementExists('xpath', '//table/tbody/tr[1]/td[1]/a');
  $this
    ->assertEquals($host_node
    ->label(), $first_row_title_link
    ->getText());
  $this
    ->assertStringContainsString($host_node
    ->toUrl()
    ->toString(), $first_row_title_link
    ->getAttribute('href'));
  $first_row_type = $this
    ->xpath('//table/tbody/tr[1]/td[2]')[0];
  $this
    ->assertEquals('Content', $first_row_type
    ->getText());
  $first_row_langcode = $this
    ->xpath('//table/tbody/tr[1]/td[3]')[0];
  $this
    ->assertEquals('English', $first_row_langcode
    ->getText());
  $first_row_field_label = $this
    ->xpath('//table/tbody/tr[1]/td[4]')[0];
  $this
    ->assertEquals('Related Views', $first_row_field_label
    ->getText());
  $first_row_status = $this
    ->xpath('//table/tbody/tr[1]/td[5]')[0];
  $this
    ->assertEquals('Published', $first_row_status
    ->getText());
}