View source
<?php
namespace Drupal\Tests\monitoring\FunctionalJavascript;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\file\Entity\File;
use Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\monitoring\Entity\SensorConfig;
use Drupal\Tests\monitoring\Functional\MonitoringTestTrait;
class MonitoringUiJavascriptTest extends WebDriverTestBase {
use MonitoringTestTrait;
protected $minkDefaultDriverClass = DrupalSelenium2Driver::class;
public static $modules = [
'block',
'monitoring',
'monitoring_test',
'dblog',
'node',
'views',
'file',
'automated_cron',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'page',
));
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('local_actions_block');
}
public function testSensorCreation() {
$account = $this
->drupalCreateUser([
'administer monitoring',
'monitoring reports',
]);
$this
->drupalLogin($account);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$assert_session
->checkboxChecked('status');
$page
->fillField('Label', 'Node Entity Aggregator sensor');
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,
];
$node_fields = [
'langcode',
'sticky',
'status',
'uuid',
'created',
'changed',
'uid',
];
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',
]));
$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_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);
}
$file_path = 'public://test';
$contents = "some content here!!.";
file_put_contents($file_path, $contents);
$this
->assertTrue(is_file($file_path));
$file = File::create([
'uri' => $file_path,
'uid' => 1,
]);
$file
->save();
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'File Entity Aggregator sensor');
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();
$file_fields = [
'uuid',
'filename',
'uri',
'filemime',
'filesize',
'status',
'created',
];
$edit = [];
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',
]));
$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_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.');
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'UI created Sensor config');
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');
$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',
]));
$this
->drupalGet('admin/config/system/monitoring/sensors/ui_created_sensor_config');
$option = $assert_session
->optionExists('Expected value type', 'bool');
$this
->assertTrue($option
->hasAttribute('selected'));
$this
->drupalPostForm(NULL, [
'settings[key]' => 'id',
'settings[config]' => 'views.view.content',
], 'Save');
$sensor_config = SensorConfig::load('ui_created_sensor_config');
$dependencies = $sensor_config
->get('dependencies');
$this
->assertEquals([
'views.view.content',
], $dependencies['config']);
$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());
$this
->drupalPostForm('admin/config/system/monitoring/sensors', [
'sensors[core_cron_safe_threshold]' => FALSE,
'sensors[core_theme_default]' => TRUE,
], 'Update enabled sensors');
$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());
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'Watchdog sensor');
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');
$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".');
$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']);
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'Test entity id');
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.');
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$page
->fillField('Label', 'Test entity id');
sleep(1);
$assert_session
->pageTextContains('Machine name: test_entity_id');
$page
->selectFieldOption('Sensor Plugin', 'entity_aggregator');
$assert_session
->assertWaitOnAjaxRequest();
$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');
$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');
}
public function testSensorInstalledModulesUI() {
$account = $this
->drupalCreateUser([
'administer monitoring',
]);
$this
->drupalLogin($account);
$page = $this
->getSession()
->getPage();
$this
->drupalGet('admin/config/system/monitoring/sensors/monitoring_installed_modules');
monitoring_sensor_manager()
->enableSensor('monitoring_installed_modules');
$this
->drupalPostForm('admin/config/system/monitoring/sensors/monitoring_installed_modules', [
'status' => TRUE,
], 'Save');
monitoring_sensor_manager()
->resetCache();
$result = monitoring_sensor_run('monitoring_installed_modules');
$this
->assertTrue($result
->isOk());
$this
->drupalPostForm('admin/config/system/monitoring/sensors/monitoring_installed_modules', [
'settings[modules][contact]' => TRUE,
'settings[modules][book]' => TRUE,
], 'Save');
monitoring_sensor_manager()
->resetCache();
$sensor_config = SensorConfig::load('monitoring_installed_modules');
$this
->assertTrue(!array_key_exists('extended', $sensor_config->settings), 'Do not persist extended module hidden selections separately.');
$result = $this
->runSensor('monitoring_installed_modules');
$this
->assertTrue($result
->isCritical());
$this
->assertEquals('2 modules delta, expected 0, Following modules are expected to be installed: Book (book), Contact (contact)', $result
->getMessage());
$this
->assertEquals(2, $result
->getValue());
$this
->drupalGet('admin/config/system/monitoring/sensors/monitoring_installed_modules');
$page
->pressButton('Update module selection');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->drupalPostForm(NULL, [], 'Save');
$result = $this
->runSensor('monitoring_installed_modules');
$this
->assertTrue($result
->isOk());
$this
->assertEquals('0 modules delta', $result
->getMessage());
$this
->installModules([
'help',
]);
$result = $this
->runSensor('monitoring_installed_modules');
$this
->assertTrue($result
->isCritical());
$this
->assertEquals('1 modules delta, expected 0, Following modules are NOT expected to be installed: Help (help)', $result
->getMessage());
$this
->assertEquals(1, $result
->getValue());
$this
->drupalPostForm('admin/config/system/monitoring/sensors/monitoring_installed_modules', [
'settings[allow_additional]' => 1,
], 'Save');
$result = $this
->runSensor('monitoring_installed_modules');
$this
->assertTrue($result
->isOk());
$this
->assertEquals('0 modules delta', $result
->getMessage());
}
}