public function MonitoringUiJavascriptTest::testSensorCreation in Monitoring 8
Tests creation of sensor through UI.
File
- tests/
src/ FunctionalJavascript/ MonitoringUiJavascriptTest.php, line 54
Class
- MonitoringUiJavascriptTest
- Tests for the Monitoring UI.
Namespace
Drupal\Tests\monitoring\FunctionalJavascriptCode
public function testSensorCreation() {
$account = $this
->drupalCreateUser([
'administer monitoring',
'monitoring reports',
]);
$this
->drupalLogin($account);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
// Create a node to test verbose fields.
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$assert_session
->checkboxChecked('status');
// Test creation of Node entity aggregator sensor.
$page
->fillField('Label', 'Node Entity Aggregator sensor');
// Give the page time to load the machine name suggestion.
sleep(1);
$assert_session
->pageTextContains('Machine name: node_entity_aggregator_sensor');
$page
->selectFieldOption('Sensor Plugin', 'entity_aggregator');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains('Sensor plugin settings');
$page
->pressButton('Add another condition');
$assert_session
->assertWaitOnAjaxRequest();
$edit = [
'caching_time' => 100,
'description' => 'Sensor created to test UI',
'value_label' => 'Test Value',
'settings[aggregation][time_interval_field]' => 'created',
'settings[aggregation][time_interval_value]' => 86400,
'settings[entity_type]' => 'node',
'conditions[0][field]' => 'type',
'conditions[0][value]' => 'article',
'conditions[1][field]' => 'sticky',
'conditions[1][value]' => 0,
];
// Available fields for the entity type node.
$node_fields = [
'langcode',
'sticky',
'status',
'uuid',
'created',
'changed',
'uid',
];
// Add verbose fields based on node fields.
foreach ($node_fields as $i => $field) {
$page
->fillField('settings[verbose_fields][' . ($i + 2) . ']', $field);
$page
->pressButton('Add another field');
$assert_session
->assertWaitOnAjaxRequest();
}
$this
->drupalPostForm(NULL, $edit, 'Save');
$assert_session
->pageTextContains(new FormattableMarkup('Sensor @label saved.', [
'@label' => 'Node Entity Aggregator sensor',
]));
// Test details page by clicking the link in confirmation message.
$this
->clickLink('Node Entity Aggregator sensor');
$assert_session
->pageTextContains('Result');
$assert_session
->responseContains('<th>id</th>');
$assert_session
->responseContains('<th>label</th>');
$assert_session
->responseContains('<th>langcode');
$assert_session
->responseContains('<th>status</th>');
$assert_session
->responseContains('<th>sticky</th>');
// Assert that the output is correct.
$assert_session
->linkExists($node
->getTitle());
$assert_session
->linkExists($node
->getOwner()
->getDisplayName());
$this
->assertFalse($node
->isSticky());
$assert_session
->pageTextContains($node
->uuid());
$assert_session
->pageTextContains(\Drupal::service('date.formatter')
->format($node
->getCreatedTime(), 'short'));
$assert_session
->pageTextContains(\Drupal::service('date.formatter')
->format($node
->getChangedTime(), 'short'));
$this
->drupalGet('admin/config/system/monitoring/sensors/node_entity_aggregator_sensor');
$this
->createScreenshot("/tmp/edit.jpg");
$assert_session
->fieldValueEquals('caching_time', 100);
$assert_session
->fieldValueEquals('conditions[0][field]', 'type');
$assert_session
->fieldValueEquals('conditions[0][value]', 'article');
$assert_session
->fieldValueEquals('conditions[1][field]', 'sticky');
$assert_session
->fieldValueEquals('conditions[1][value]', '0');
$i = 2;
foreach ($node_fields as $field) {
$assert_session
->fieldValueEquals('settings[verbose_fields][' . $i++ . ']', $field);
}
// Create a file to test.
$file_path = 'public://test';
$contents = "some content here!!.";
file_put_contents($file_path, $contents);
// Test if the file exist.
$this
->assertTrue(is_file($file_path));
// Create a file entity.
$file = File::create([
'uri' => $file_path,
'uid' => 1,
]);
$file
->save();
// Test creation of File entity aggregator sensor.
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'File Entity Aggregator sensor');
// Give the page time to load the machine name suggestion.
sleep(1);
$assert_session
->pageTextContains('Machine name: file_entity_aggregator_sensor');
$page
->selectFieldOption('Sensor Plugin', 'entity_aggregator');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains('Sensor plugin settings');
$page
->selectFieldOption('Entity Type', 'file');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Add another condition');
$assert_session
->assertWaitOnAjaxRequest();
// Available fields for entity type file.
$file_fields = [
'uuid',
'filename',
'uri',
'filemime',
'filesize',
'status',
'created',
];
$edit = [];
// Add verbose fields based on file fields.
foreach ($file_fields as $i => $field) {
$page
->fillField('settings[verbose_fields][' . ($i + 2) . ']', $field);
$page
->pressButton('Add another field');
$assert_session
->assertWaitOnAjaxRequest();
}
$this
->drupalPostForm(NULL, $edit, 'Save');
$assert_session
->pageTextContains(new FormattableMarkup('Sensor @label saved.', [
'@label' => 'File Entity Aggregator sensor',
]));
// Test details page by clicking the link in confirmation message.
$this
->clickLink('File Entity Aggregator sensor');
$assert_session
->pageTextContains('Result');
$assert_session
->responseContains('<th>label</th>');
$assert_session
->responseContains('<th>uuid</th>');
$assert_session
->responseContains('<th>filename</th>');
$assert_session
->responseContains('<th>filesize</th>');
$assert_session
->responseContains('<th>uri</th>');
$assert_session
->responseContains('<th>created</th>');
// Assert that the output is correct.
$assert_session
->pageTextContains($file
->getFilename());
$assert_session
->pageTextContains($file
->uuid());
$assert_session
->pageTextContains($file
->getSize());
$assert_session
->pageTextContains($file
->getMimeType());
$assert_session
->pageTextContains(\Drupal::service('date.formatter')
->format($file
->getCreatedTime(), 'short'));
$this
->drupalGet('admin/config/system/monitoring/sensors/file_entity_aggregator_sensor');
$i = 2;
foreach ($file_fields as $field) {
$assert_session
->fieldValueEquals('settings[verbose_fields][' . $i++ . ']', $field);
}
$this
->drupalGet('admin/config/system/monitoring/sensors/node_entity_aggregator_sensor/delete');
$assert_session
->pageTextContains('This action cannot be undone.');
$this
->drupalPostForm(NULL, [], 'Delete');
$assert_session
->pageTextContains('Node Entity Aggregator sensor has been deleted.');
// Configuration sensor.
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'UI created Sensor config');
// Give the page time to load the machine name suggestion.
sleep(1);
$assert_session
->pageTextContains('Machine name: ui_created_sensor_config');
$page
->selectFieldOption('Sensor Plugin', 'config_value');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains('Expected value');
$assert_session
->pageTextContains('Sensor plugin settings');
// Test if the expected value type is no_value, the value label is hidden.
$page
->selectFieldOption('Expected value type', 'no_value');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$assert_session
->pageTextNotContains('The value label represents the units of the sensor value.');
$page
->selectFieldOption('Expected value type', 'bool');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains('The value label represents the units of the sensor value.');
$this
->drupalPostForm(NULL, [
'description' => 'Sensor created to test UI',
'value_label' => 'Test Value',
'caching_time' => 100,
'value_type' => 'bool',
'settings[key]' => 'interval',
'settings[config]' => 'automated_cron.settings',
], 'Save');
$assert_session
->pageTextContains(new FormattableMarkup('Sensor @label saved.', [
'@label' => 'UI created Sensor config',
]));
// Go back to the sensor edit page,
// Check the value type is properly selected.
$this
->drupalGet('admin/config/system/monitoring/sensors/ui_created_sensor_config');
$option = $assert_session
->optionExists('Expected value type', 'bool');
$this
->assertTrue($option
->hasAttribute('selected'));
// Update sensor with a config entity.
$this
->drupalPostForm(NULL, [
'settings[key]' => 'id',
'settings[config]' => 'views.view.content',
], 'Save');
// Make sure the config dependencies are set.
$sensor_config = SensorConfig::load('ui_created_sensor_config');
$dependencies = $sensor_config
->get('dependencies');
$this
->assertEquals([
'views.view.content',
], $dependencies['config']);
// Try to enable a sensor which is disabled by default and vice versa.
// Check the default status of cron safe threshold and new users sensors.
$sensor_cron = SensorConfig::load('core_cron_safe_threshold');
$this
->assertTrue($sensor_cron
->status());
$sensor_theme = SensorConfig::load('core_theme_default');
$this
->assertFalse($sensor_theme
->status());
// Change the status of these sensors.
$this
->drupalPostForm('admin/config/system/monitoring/sensors', [
'sensors[core_cron_safe_threshold]' => FALSE,
'sensors[core_theme_default]' => TRUE,
], 'Update enabled sensors');
// Make sure the changes have been made.
$sensor_cron = SensorConfig::load('core_cron_safe_threshold');
$this
->assertFalse($sensor_cron
->status());
$sensor_theme = SensorConfig::load('core_theme_default');
$this
->assertTrue($sensor_theme
->status());
// Test the creation of a Watchdog sensor with default configuration.
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'Watchdog sensor');
// Give the page time to load the machine name suggestion.
sleep(1);
$assert_session
->pageTextContains('Machine name: watchdog_sensor');
$page
->selectFieldOption('Sensor Plugin', 'watchdog_aggregator');
$assert_session
->assertWaitOnAjaxRequest();
$this
->drupalPostForm(NULL, [], 'Save');
$assert_session
->pageTextContains('Sensor Watchdog Sensor saved');
// Edit sensor with invalid fields.
$this
->drupalPostForm('admin/config/system/monitoring/sensors/watchdog_sensor', [
'conditions[0][field]' => 'condition_invalid',
'verbose_fields[0][field_key]' => 'verbose_invalid',
], 'Save');
$assert_session
->pageTextContains('The field condition_invalid does not exist in the table "watchdog".');
$assert_session
->pageTextContains('The field verbose_invalid does not exist in the table "watchdog".');
// Load the created sensor and assert the default configuration.
$sensor_config = SensorConfig::load('watchdog_sensor');
$settings = $sensor_config
->getSettings();
$this
->assertEquals('number', $sensor_config
->getValueType());
$this
->assertEquals('watchdog', $settings['table']);
$this
->assertEquals('timestamp', $settings['time_interval_field']);
// Test that the entity id is set after selecting a watchdog sensor.
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'Test entity id');
// Give the page time to load the machine name suggestion.
sleep(1);
$assert_session
->pageTextContains('Machine name: test_entity_id');
$page
->selectFieldOption('Sensor Plugin', 'watchdog_aggregator');
$assert_session
->assertWaitOnAjaxRequest();
$page
->selectFieldOption('Sensor Plugin', 'entity_aggregator');
$assert_session
->assertWaitOnAjaxRequest();
$this
->drupalPostForm(NULL, [], 'Save');
$assert_session
->pageTextContains('Sensor Test entity id saved.');
// Test that the description of the verbose output changes.
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'Test entity id');
// Give the page time to load the machine name suggestion.
sleep(1);
$assert_session
->pageTextContains('Machine name: test_entity_id');
$page
->selectFieldOption('Sensor Plugin', 'entity_aggregator');
$assert_session
->assertWaitOnAjaxRequest();
// Change entity type to File.
$page
->selectFieldOption('Entity Type', 'file');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains('Available Fields for entity type File:');
$assert_session
->pageTextContains('changed, created, fid, filemime, filename, filesize, id, label, langcode, status, uid, uri, uuid');
// Change entity type to User.
$page
->selectFieldOption('Entity Type', 'user');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains('Available Fields for entity type User:');
$assert_session
->pageTextContains('access, changed, created, default_langcode, id, init, label, langcode, login, mail, name, pass, preferred_admin_langcode, preferred_langcode, roles, status, timezone, uid, uuid');
}