You are here

public function MonitoringCoreWebTest::testEntityAggregator in Monitoring 8

Tests the entity aggregator.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\ContentEntityAggregatorSensorPlugin

File

tests/src/Functional/MonitoringCoreWebTest.php, line 554

Class

MonitoringCoreWebTest
Integration tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Functional

Code

public function testEntityAggregator() {

  // Create content types and nodes.
  $type1 = $this
    ->drupalCreateContentType();
  $type2 = $this
    ->drupalCreateContentType();
  $sensor_config = SensorConfig::load('entity_aggregate_test');
  $node1 = $this
    ->drupalCreateNode(array(
    'type' => $type1
      ->id(),
  ));
  $node2 = $this
    ->drupalCreateNode(array(
    'type' => $type2
      ->id(),
  ));
  $this
    ->drupalCreateNode(array(
    'type' => $type2
      ->id(),
  ));

  // One node should not meet the time_interval condition.
  $node = $this
    ->drupalCreateNode(array(
    'type' => $type2
      ->id(),
  ));
  \Drupal::database()
    ->update('node_field_data')
    ->fields(array(
    'created' => \Drupal::time()
      ->getRequestTime() - ($sensor_config
      ->getTimeIntervalValue() + 10),
  ))
    ->condition('nid', $node
    ->id())
    ->execute();

  // Test for the node type1.
  $sensor_config = SensorConfig::load('entity_aggregate_test');
  $sensor_config->settings['conditions'] = array(
    'test' => array(
      'field' => 'type',
      'value' => $type1
        ->id(),
    ),
  );
  $sensor_config
    ->save();
  $result = $this
    ->runSensor('entity_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), '1');

  // Test for node type2.
  $sensor_config->settings['conditions'] = array(
    'test' => array(
      'field' => 'type',
      'value' => $type2
        ->id(),
    ),
  );
  $sensor_config
    ->save();
  $result = $this
    ->runSensor('entity_aggregate_test');

  // There should be two nodes with node type2 and created in last 24 hours.
  $this
    ->assertEqual($result
    ->getValue(), 2);

  // Test support for configurable fields, create a taxonomy reference field.
  $vocabulary = $this
    ->createVocabulary();
  FieldStorageConfig::create(array(
    'field_name' => 'term_reference',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'entity_types' => array(
      'node',
    ),
    'settings' => array(
      'target_type' => 'taxonomy_term',
    ),
  ))
    ->save();
  FieldConfig::create(array(
    'label' => 'Term reference',
    'field_name' => 'term_reference',
    'entity_type' => 'node',
    'bundle' => $type2
      ->id(),
    'settings' => array(
      'bundles' => [
        $vocabulary
          ->id() => $vocabulary
          ->id(),
      ],
    ),
    'required' => FALSE,
  ))
    ->save();
  FieldStorageConfig::create(array(
    'field_name' => 'term_reference2',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'entity_types' => array(
      'node',
    ),
    'settings' => array(
      'target_type' => 'taxonomy_term',
    ),
  ))
    ->save();
  FieldConfig::create(array(
    'label' => 'Term reference 2',
    'field_name' => 'term_reference2',
    'entity_type' => 'node',
    'bundle' => $type2
      ->id(),
    'settings' => array(
      'bundles' => [
        $vocabulary
          ->id() => $vocabulary
          ->id(),
      ],
    ),
    'required' => FALSE,
  ))
    ->save();

  // Create some terms.
  $term1 = $this
    ->createTerm($vocabulary);
  $term2 = $this
    ->createTerm($vocabulary);

  // Create node that only references the first term.
  $node1 = $this
    ->drupalCreateNode(array(
    'created' => \Drupal::time()
      ->getRequestTime(),
    'type' => $type2
      ->id(),
    'term_reference' => array(
      array(
        'target_id' => $term1
          ->id(),
      ),
    ),
  ));

  // Create node that only references both terms.
  $node2 = $this
    ->drupalCreateNode(array(
    'created' => \Drupal::time()
      ->getRequestTime(),
    'type' => $type2
      ->id(),
    'term_reference' => array(
      array(
        'target_id' => $term1
          ->id(),
      ),
      array(
        'target_id' => $term2
          ->id(),
      ),
    ),
  ));

  // Create a third node that references both terms but in different fields.
  $node3 = $this
    ->drupalCreateNode(array(
    'created' => \Drupal::time()
      ->getRequestTime(),
    'type' => $type2
      ->id(),
    'term_reference' => array(
      array(
        'target_id' => $term1
          ->id(),
      ),
    ),
    'term_reference2' => array(
      array(
        'target_id' => $term2
          ->id(),
      ),
    ),
  ));

  // Update the sensor to look for nodes with a reference to term1 in the
  // first field.
  $sensor_config->settings['conditions'] = array(
    'test' => array(
      'field' => 'term_reference.target_id',
      'value' => $term1
        ->id(),
    ),
  );
  $sensor_config->settings['entity_type'] = 'node';
  $sensor_config
    ->save();
  $result = $this
    ->runSensor('entity_aggregate_test');

  // There should be three nodes with that reference.
  $this
    ->assertEqual($result
    ->getValue(), 3);

  // Check the content entity aggregator verbose output and other UI elements.
  $this
    ->drupalLogin($this
    ->createUser([
    'monitoring reports',
    'administer monitoring',
  ]));
  $this
    ->drupalPostForm('admin/reports/monitoring/sensors/entity_aggregate_test', [], t('Run now'));
  $this
    ->assertText('id');
  $this
    ->assertText('label');
  $this
    ->assertLink($node1
    ->label());
  $this
    ->assertLink($node2
    ->label());
  $this
    ->assertLink($node3
    ->label());

  // Assert Query result appears.
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->elementTextContains('css', '#result', 'base_table');

  // Check timestamp is formated correctly.
  $timestamp = \Drupal::service('date.formatter')
    ->format($node1
    ->getCreatedTime(), 'short');
  $assert_session
    ->elementTextContains('css', '#result tbody tr:nth-child(2) td:nth-child(3)', $timestamp);
  $this
    ->clickLink(t('Edit'));

  // Assert some of the 'available fields'.
  $this
    ->assertText('Available Fields for entity type Content: changed, created, default_langcode, id, label, langcode, nid, promote, revision_default, revision_log, revision_timestamp, revision_translation_affected, revision_uid, status, sticky, title, type, uid, uuid, vid.');
  $this
    ->assertFieldByName('conditions[0][field]', 'term_reference.target_id');
  $this
    ->assertFieldByName('conditions[0][value]', $term1
    ->id());

  // Test adding another field.
  $this
    ->drupalPostForm(NULL, [
    'settings[verbose_fields][2]' => 'revision_timestamp',
  ], t('Add another field'));

  // Repeat for a condition, add an invalid field while we are at it.
  $this
    ->drupalPostForm(NULL, [
    'conditions[1][field]' => 'nid',
    'conditions[1][operator]' => '>',
    'conditions[1][value]' => 4,
    // The invalid field.
    'settings[verbose_fields][3]' => 'test_wrong_field',
  ], t('Add another condition'));
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->clickLink('Entity Aggregate test');

  // Assert the new field and it's formatted output.
  $this
    ->assertText('revision_timestamp');
  $this
    ->assertText(\Drupal::service('date.formatter')
    ->format($node1
    ->getRevisionCreationTime(), 'short'));
  $this
    ->assertText(\Drupal::service('date.formatter')
    ->format($node2
    ->getRevisionCreationTime(), 'short'));
  $this
    ->assertText(\Drupal::service('date.formatter')
    ->format($node3
    ->getRevisionCreationTime(), 'short'));

  // Update the sensor to look for nodes with a reference to term1 in the
  // first field and term2 in the second.
  $sensor_config->settings['conditions'] = array(
    'test' => array(
      'field' => 'term_reference.target_id',
      'value' => $term1
        ->id(),
    ),
    'test2' => array(
      'field' => 'term_reference2.target_id',
      'value' => $term2
        ->id(),
    ),
  );
  $sensor_config
    ->save();
  $result = $this
    ->runSensor('entity_aggregate_test');

  // There should be one nodes with those references.
  $this
    ->assertEqual($result
    ->getValue(), 1);
}