diff --git a/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml b/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml index 171c7aa..3655608 100644 --- a/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml +++ b/modules/test/config/optional/monitoring.sensor_config.entity_aggregate_test.yml @@ -1,6 +1,6 @@ id: entity_aggregate_test -label: 'DB Aggregate test' -description: 'Tests the generic DB aggregate sensor.' +label: 'Entity Aggregate test' +description: 'Tests the content entity aggregate sensor.' category: 'Test' plugin_id: entity_aggregator value_type : 'number' diff --git a/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php index 7eca020..5594e85 100644 --- a/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php +++ b/src/Plugin/monitoring/SensorPlugin/ContentEntityAggregatorSensorPlugin.php @@ -250,7 +250,7 @@ class ContentEntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin $row[] = $value ? \Drupal::service('renderer')->renderPlain($value) : $entity->$field->$property; } else { - $row[] = t('invalid field'); + $row[] = t('field %field is invalid', ['%field' => $field]); } break; } @@ -262,7 +262,7 @@ class ContentEntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin ); } if (count($rows) > 0) { - $header = $this->sensorConfig->getSetting('verbose_fields', ['ID, Label']); + $header = $this->sensorConfig->getSetting('verbose_fields', ['id', 'label']); $output['entities'] = array( '#type' => 'table', '#header' => $header, @@ -300,10 +300,10 @@ class ContentEntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin $settings = $this->sensorConfig->getSettings(); $entity_types = $this->entityManager->getEntityTypeLabels(); $options = []; - foreach (array_keys($entity_types) as $delta => $type) { - $class = $this->entityManager->getDefinition($type)->getClass(); + foreach ($entity_types as $id => $label) { + $class = $this->entityManager->getDefinition($id)->getClass(); if (is_subclass_of($class, '\Drupal\Core\Entity\FieldableEntityInterface', TRUE)) { - $options[array_keys($entity_types)[$delta]] = array_values($entity_types)[$delta]; + $options[$id] = $label; }; } @@ -344,7 +344,7 @@ class ContentEntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin ); // Fill the sensors table with form elements for each sensor. - $conditions = $this->sensorConfig->getSetting('conditions'); + $conditions = array_values($this->sensorConfig->getSetting('conditions')); if (empty($conditions)) { $conditions = []; } @@ -433,8 +433,7 @@ class ContentEntityAggregatorSensorPlugin extends DatabaseAggregatorSensorPlugin '#markup' => t('Available Fields for entity %type: %fields', [ '%type' => $entity_type->getLabel(), '%fields' => implode(', ', array_keys($this->entityManager->getBaseFieldDefinitions($this->getEntityTypeId()))) - ] - ), + ]), ]; // Fill the sensors table with form elements for each sensor. diff --git a/src/Tests/MonitoringCoreWebTest.php b/src/Tests/MonitoringCoreWebTest.php index 921d9d0..825b801 100644 --- a/src/Tests/MonitoringCoreWebTest.php +++ b/src/Tests/MonitoringCoreWebTest.php @@ -379,14 +379,14 @@ class MonitoringCoreWebTest extends MonitoringTestBase { $term2 = $this->createTerm($vocabulary); // Create node that only references the first term. - $this->drupalCreateNode(array( + $node1 = $this->drupalCreateNode(array( 'created' => REQUEST_TIME, 'type' => $type2->id(), 'term_reference' => array(array('target_id' => $term1->id())), )); // Create node that only references both terms. - $this->drupalCreateNode(array( + $node2 = $this->drupalCreateNode(array( 'created' => REQUEST_TIME, 'type' => $type2->id(), 'term_reference' => array( @@ -396,7 +396,7 @@ class MonitoringCoreWebTest extends MonitoringTestBase { )); // Create a third node that references both terms but in different fields. - $this->drupalCreateNode(array( + $node3 = $this->drupalCreateNode(array( 'created' => REQUEST_TIME, 'type' => $type2->id(), 'term_reference' => array(array('target_id' => $term1->id())), @@ -414,6 +414,43 @@ class MonitoringCoreWebTest extends MonitoringTestBase { // 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()); + $this->clickLink(t('Edit')); + // Assert some of the 'available fields'. + $this->assertText('Content: nid, uuid, vid, type, langcode, title, uid, status, created, changed, promote, sticky, revision_timestamp, revision_uid,'); + $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')); + $this->assertText('field test_wrong_field is invalid'); + // 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(