View source
<?php
namespace Drupal\Tests\monitoring\Functional;
use Behat\Mink\Element\NodeElement;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\monitoring\Entity\SensorConfig;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
class MonitoringCoreWebTest extends MonitoringTestBase {
public static $modules = array(
'dblog',
'image',
'node',
'taxonomy',
'file',
);
public function testSensors() {
$this
->doTestUserIntegritySensorPlugin();
$this
->doTestDatabaseAggregatorSensorPluginActiveSessions();
$this
->doTestTwigDebugSensor();
$this
->doTestWatchdogAggregatorSensorPlugin();
$this
->doTestPhpNoticesSensor();
$this
->doTestQueueSizeSensor();
}
public function doTestQueueSizeSensor() {
$account = $this
->drupalCreateUser([
'administer monitoring',
'monitoring reports',
]);
$this
->drupalLogin($account);
$this
->drupalGet('admin/config/system/monitoring/sensors/add');
$this
->assertFieldByName('status', TRUE);
$this
->drupalPostForm('admin/config/system/monitoring/sensors/add', [
'label' => 'QueueTest',
'id' => 'queue_size_test',
'plugin_id' => 'queue_size',
], 'Select sensor');
$this
->assertOption('edit-settings-queue', 'monitoring_test');
$this
->assertOptionByText('edit-settings-queue', 'Test Worker');
$edit = [
'settings[queue]' => 'monitoring_test',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertText('Sensor QueueTest saved.');
}
public function testUpdateRequirementsSensors() {
$test_user = $this
->drupalCreateUser([
'monitoring reports',
'administer monitoring',
]);
$this
->drupalLogin($test_user);
$this
->drupalGet('admin/reports/monitoring');
$this
->assertNoRaw('<span title="Requirements of the past module">Module past</span>');
$this
->installModules([
'past',
]);
$this
->drupalGet('admin/reports/monitoring');
$this
->assertRaw('<span title="Requirements of the past module">Module past</span>');
$this
->uninstallModules([
'past',
]);
$this
->drupalGet('admin/reports/monitoring');
$this
->assertNoRaw('<span title="Requirements of the past module">Module past</span>');
$this
->drupalGet('/admin/config/system/monitoring/sensors/rebuild');
$this
->assertText('No changes were made.');
}
protected function doTestWatchdogAggregatorSensorPlugin() {
$test_user = $this
->drupalCreateUser([
'administer site configuration',
'administer monitoring',
'monitoring reports',
'access site reports',
'monitoring verbose',
]);
$this
->drupalLogin($test_user);
$this
->drupalGet('admin/reports/monitoring/sensors/user_successful_logins');
$rows = $this
->getSession()
->getPage()
->findAll('css', '#unaggregated_result tbody tr');
$message = $rows[0]
->find('css', 'td:nth-child(2)')
->getText();
$this
->assertEquals(5, count($rows), 'There are 5 results in the table.');
$this
->assertTrue(!empty($rows[0]
->find('css', 'a')
->getText()), 'Found WID in verbose output');
$this
->assertEquals("Session opened for {$test_user->getDisplayName()}.", $message, 'Found replaced message in output.');
$this
->assertText('Session opened for ' . $test_user
->label());
$this
->drupalPostForm('admin/config/system/monitoring/sensors/user_successful_logins', [
'verbose_fields[variables][field_key]' => '',
], t('Save'));
$this
->drupalGet('admin/reports/monitoring/sensors/user_successful_logins');
$rows = $this
->getSession()
->getPage()
->findAll('css', '#unaggregated_result tbody tr');
$message = $rows[0]
->find('css', 'td:nth-child(2)')
->getText();
$this
->assertTrue(!empty($rows[0]
->find('css', 'td:nth-child(1)')
->getText()), 'Found WID in verbose output');
$this
->assertEquals('Session opened for %name.', $message, 'Found unreplaced message in output.');
}
protected function doTestDatabaseAggregatorSensorPluginActiveSessions() {
$test_user = $this
->drupalCreateUser([
'monitoring reports',
'access site reports',
'monitoring verbose',
]);
$this
->drupalLogin($test_user);
$result = $this
->runSensor('user_sessions_authenticated');
$this
->assertEqual($result
->getValue(), 1);
$result = $this
->runSensor('user_sessions_all');
$this
->assertEqual($result
->getValue(), 1);
$this
->drupalLogout();
$result = $this
->runSensor('user_sessions_authenticated');
$this
->assertEqual($result
->getValue(), 0);
$result = $this
->runSensor('user_sessions_all');
$this
->assertEqual($result
->getValue(), 0);
$this
->drupalLogin($test_user);
$test_user = User::load($test_user
->id());
$this
->drupalGet('/admin/reports/monitoring/sensors/user_sessions_authenticated');
if (\version_compare(\Drupal::VERSION, '9', '>=')) {
$query = 'SELECT "sessions"."uid" AS "uid", "sessions"."hostname" AS "hostname", "sessions"."timestamp" AS "timestamp" FROM "' . $this->databasePrefix . 'sessions" "sessions" WHERE ("uid" != :db_condition_placeholder_0) AND ("timestamp" > :db_condition_placeholder_1) ORDER BY "timestamp" DESC LIMIT 10 OFFSET 0';
}
else {
$query = "SELECT sessions.uid AS uid, sessions.hostname AS hostname, sessions.timestamp AS timestamp FROM {$this->databasePrefix}sessions sessions WHERE (uid != :db_condition_placeholder_0) AND (timestamp > :db_condition_placeholder_1) ORDER BY timestamp DESC LIMIT 10 OFFSET 0";
}
$this
->assertSession()
->elementTextContains('css', '#unaggregated_result details pre', $query);
$columns = $this
->getSession()
->getPage()
->findAll('css', '#unaggregated_result tbody tr:nth-child(1) td');
$this
->assertTrue(count($columns) == 3, '3 fields have been found in the verbose result.');
$this
->assertSession()
->elementTextContains('css', '#history tbody tr:nth-child(1) td:nth-child(2)', '1', 'record_count found in History.');
$expected_time = \Drupal::service('date.formatter')
->format(floor($test_user
->getLastLoginTime() / 86400) * 86400, 'short');
$this
->assertSession()
->elementTextContains('css', '#history tbody tr:nth-child(1) td:nth-child(1)', $expected_time);
$this
->drupalGet('/admin/reports/monitoring/sensors/dblog_event_severity_notice');
$this
->assertText('Session opened for ' . $test_user
->label());
$this
->drupalGet('/admin/reports/monitoring/sensors/dblog_event_severity_warning');
$this
->assertText('There are no results for this sensor to display.');
}
public function doTestTwigDebugSensor() {
$result = $this
->runSensor('twig_debug_mode');
$this
->assertTrue($result
->isOk());
$this
->assertEqual($result
->getMessage(), 'Optimal configuration');
$twig_config = $this->container
->getParameter('twig.config');
$twig_config['debug'] = FALSE;
$twig_config['cache'] = TRUE;
$twig_config['auto_reload'] = NULL;
$this
->setContainerParameter('twig.config', $twig_config);
$this
->rebuildContainer();
$result = $this
->runSensor('twig_debug_mode');
$this
->assertTrue($result
->isOk());
$this
->assertEqual($result
->getMessage(), 'Optimal configuration');
$twig_config = $this->container
->getParameter('twig.config');
$twig_config['debug'] = TRUE;
$twig_config['cache'] = FALSE;
$twig_config['auto_reload'] = TRUE;
$this
->setContainerParameter('twig.config', $twig_config);
$this
->rebuildContainer();
$result = $this
->runSensor('twig_debug_mode');
$this
->assertTrue($result
->isWarning());
$this
->assertEqual($result
->getMessage(), 'Twig debug mode is enabled, Twig cache disabled, Automatic recompilation of Twig templates enabled');
}
protected function doTestUserIntegritySensorPlugin() {
$test_user_first = $this
->drupalCreateUser(array(
'administer monitoring',
), 'test_user');
$this
->runSensor('user_integrity');
$test_user_first
->delete();
$result = $this
->runSensor('user_integrity');
$this
->assertTrue($result
->isOk());
$test_user_first = $this
->drupalCreateUser(array(
'administer monitoring',
), 'test_user_1');
$this
->drupalLogin($test_user_first);
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '1 privileged user(s), 1 new user(s)');
$test_user_second = $this
->drupalCreateUser(array(), 'test_user_2', TRUE);
$this
->drupalLogin($test_user_second);
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '2 privileged user(s), 2 new user(s)');
\Drupal::keyValue('monitoring.users')
->deleteAll();
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '2 privileged user(s)');
$test_user_second
->setUsername('changed');
$test_user_second
->save();
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '2 privileged user(s), 1 changed user(s)');
\Drupal::keyValue('monitoring.users')
->deleteAll();
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '2 privileged user(s)');
\Drupal::configFactory()
->getEditable('user.settings')
->set('register', 'admin_only')
->save();
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, array(
'administer account settings',
));
\Drupal::keyValue('monitoring.users')
->deleteAll();
$result = $this
->runSensor('user_integrity');
$this
->assertTrue($result
->isWarning());
$this
->assertEqual($result
->getMessage(), '3 privileged user(s), Privileged access for roles Authenticated user');
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
'administer account settings',
));
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '3 privileged user(s), Privileged access for roles Anonymous user, Authenticated user');
\Drupal::configFactory()
->getEditable('user.settings')
->set('register', 'visitors')
->save();
$result = $this
->runSensor('user_integrity');
$this
->assertTrue($result
->isCritical());
$this
->assertEqual($result
->getMessage(), '3 privileged user(s), Privileged access for roles Anonymous user, Authenticated user, Self registration possible.');
$test_user_third = $this
->drupalCreateUser(array(), 'test_user_3');
\Drupal::keyValue('monitoring.users')
->deleteAll();
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '4 privileged user(s), Privileged access for roles Anonymous user, Authenticated user, Self registration possible.');
$test_user_third
->setUsername('changed2');
$test_user_third
->save();
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '4 privileged user(s), 1 changed user(s), Privileged access for roles Anonymous user, Authenticated user, Self registration possible.');
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, array(
'administer account settings',
));
user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, array(
'administer account settings',
));
\Drupal::keyValue('monitoring.users')
->deleteAll();
$result = $this
->runSensor('user_integrity');
$this
->assertEqual($result
->getMessage(), '2 privileged user(s)');
}
protected function doTestPhpNoticesSensor() {
$test_user_first = $this
->drupalCreateUser(array(
'administer monitoring',
'monitoring reports',
'monitoring verbose',
), 'test_user_php');
$this
->drupalLogin($test_user_first);
$error = [
'%type' => 'Recoverable fatal error',
'@message' => 'Argument 1 passed to Drupal\\Core\\Form\\ConfigFormBase::buildForm() must be of the type array, null given, called in /usr/local/var/www/d8/www/core/modules/system/src/Form/CronForm.php on line 127 and defined',
'%function' => 'Drupal\\Core\\Form\\ConfigFormBase->buildForm()',
'%line' => '42',
'%file' => DRUPAL_ROOT . '/core/lib/Drupal/Core/Form/ConfigFormBase.php',
'severity_level' => 3,
];
$new_error = [
'%type' => 'Notice',
'@message' => 'Use of undefined constant B - assumed \'B\'',
'%function' => 'Drupal\\system\\Form\\CronForm->buildForm()',
'%line' => '126',
'%file' => DRUPAL_ROOT . '/core/modules/system/src/Form/CronForm.php',
'severity_level' => 5,
];
\Drupal::logger('php')
->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
\Drupal::logger('php')
->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
\Drupal::logger('php')
->log($new_error['severity_level'], '%type: @message in %function (line %line of %file).', $new_error);
$this
->drupalGet('/admin/reports/monitoring/sensors/dblog_php_notices');
$expected_header = [
'count',
'type',
'message',
'caller',
'file',
];
$expected_body_one = [
'2',
'Recoverable fatal error',
'Argument 1 passed to Drupal\\Core\\Form\\ConfigFormBase::buildForm() must be of the type array, null given, called in /usr/local/var/www/d8/www/core/modules/system/src/Form/CronForm.php on line 127 and defined',
'Drupal\\Core\\Form\\ConfigFormBase->buildForm()',
'core/lib/Drupal/Core/Form/ConfigFormBase.php:42',
];
$expected_body_two = [
'1',
'Notice',
'Use of undefined constant B - assumed \'B\'',
'Drupal\\system\\Form\\CronForm->buildForm()',
'core/modules/system/src/Form/CronForm.php:126',
];
$convert_to_array = function (NodeElement $header) {
return $header
->getText();
};
$this
->drupalPostForm('/admin/reports/monitoring/sensors/dblog_php_notices', [], t('Run now'));
$headers = $this
->getSession()
->getPage()
->findAll('css', '#unaggregated_result thead tr th');
$headers = array_map($convert_to_array, $headers);
$this
->assertEquals($expected_header, $headers, 'The header is correct.');
$rows = $this
->getSession()
->getPage()
->findAll('css', '#unaggregated_result tbody tr');
$this
->assertEquals(2, count($rows), 'Two PHP notices were logged.');
$first_message = array_map($convert_to_array, $rows[0]
->findAll('css', 'td'));
$second_message = array_map($convert_to_array, $rows[1]
->findAll('css', 'td'));
$this
->assertEqual($first_message, $expected_body_one, 'The first notice is as expected.');
$this
->assertEqual($second_message, $expected_body_two, 'The second notice is as expected');
$this
->assertEqual(str_replace(DRUPAL_ROOT . '/', '', $error['%file'] . ':' . $error['%line']), $first_message[4], 'Filename was successfully shortened.');
}
public function testUserFailedLoginSensorPlugin() {
$this
->drupalPostForm('user/login', [
'name' => 'admin',
'pass' => '123',
], t('Log in'));
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('admin/reports/monitoring/sensors/user_failed_logins');
$rows = $this
->getSession()
->getPage()
->findAll('css', '#unaggregated_result tbody tr');
$this
->assertEquals(1, count($rows), 'Found 1 results in table');
$this
->assertSession()
->elementTextContains('css', '#unaggregated_result tbody tr:nth-child(1) td:nth-child(2)', 'Login attempt failed for admin');
$wid = $rows[0]
->find('css', 'td:nth-child(1) a')
->getText();
$query = \Drupal::database()
->select('watchdog');
$query
->addField('watchdog', 'timestamp');
$query
->condition('wid', $wid);
$result = $query
->range(0, 10)
->execute()
->fetchObject();
$expected_time = \Drupal::service('date.formatter')
->format($result->timestamp, 'short');
$this
->assertSession()
->elementTextContains('css', '#unaggregated_result tbody tr:nth-child(1) td:nth-child(3)', $expected_time);
}
public function testNonExistingUserFailedLoginSensorPlugin() {
\Drupal::database()
->insert('watchdog')
->fields(array(
'type' => 'user',
'message' => 'Login attempt failed from %ip.',
'variables' => serialize([
'%ip' => '127.0.0.1',
]),
'location' => 'http://example.com/user/login',
'timestamp' => \Drupal::time()
->getRequestTime(),
))
->execute();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('admin/reports/monitoring/sensors/user_void_failed_logins');
$rows = $this
->getSession()
->getPage()
->findAll('css', '#unaggregated_result tbody tr');
$this
->assertEquals(1, count($rows), 'Found 1 results in table');
$this
->assertSession()
->elementTextContains('css', '#unaggregated_result tbody tr:nth-child(1) td:nth-child(2)', 'Login attempt failed from 127.0.0.1');
}
public function testSensorDisappearedSensors() {
$this
->installModules(array(
'media',
));
$result = $this
->runSensor('monitoring_disappeared_sensors');
$this
->assertTrue($result
->isOk());
$log = $this
->loadWatchdog();
$this
->assertEqual(count($log), 1, 'There should be one log entry: all sensors enabled by default added.');
$sensor_config_all = monitoring_sensor_manager()
->getAllSensorConfig();
$this
->assertEqual(new FormattableMarkup($log[0]->message, unserialize($log[0]->variables)), new FormattableMarkup('@count new sensor/s added: @names', array(
'@count' => count($sensor_config_all),
'@names' => implode(', ', array_keys($sensor_config_all)),
)));
$this
->uninstallModules(array(
'media',
));
$result = $this
->runSensor('monitoring_disappeared_sensors');
$this
->assertTrue($result
->isCritical());
$this
->assertEqual($result
->getMessage(), 'Missing sensor core_requirements_media');
$this
->assertEqual(count($this
->loadWatchdog()), 1);
$this
->installModules(array(
'media',
));
monitoring_sensor_manager()
->enableSensor('core_requirements_media');
$result = $this
->runSensor('monitoring_disappeared_sensors');
$this
->assertTrue($result
->isOk());
$this
->assertEqual(count($this
->loadWatchdog()), 1);
monitoring_sensor_manager()
->disableSensor('core_requirements_media');
$this
->uninstallModules(array(
'media',
));
$result = $this
->runSensor('monitoring_disappeared_sensors');
$this
->assertTrue($result
->isOk());
$log = $this
->loadWatchdog();
$this
->assertEqual(count($log), 2, 'Removal of core_requirements_media sensor should be logged.');
$this
->assertEqual(new FormattableMarkup($log[1]->message, unserialize($log[1]->variables)), '1 new sensor/s removed: core_requirements_media');
}
public function testSensorInstalledModulesAPI() {
$result = $this
->runSensor('monitoring_installed_modules');
$this
->assertTrue($result
->isOk());
$this
->installModules(array(
'contact',
));
$result = $this
->runSensor('monitoring_installed_modules');
$this
->assertTrue($result
->isCritical());
$this
->assertEqual($result
->getMessage(), '1 modules delta, expected 0, Following modules are NOT expected to be installed: Contact (contact)');
$this
->assertEqual($result
->getValue(), 1);
$sensor_config = SensorConfig::load('monitoring_installed_modules');
$sensor_config->settings['allow_additional'] = TRUE;
$sensor_config
->save();
$result = $this
->runSensor('monitoring_installed_modules');
$this
->assertTrue($result
->isOk());
$sensor_config->settings['modules']['contact'] = 'contact';
$sensor_config
->save();
$this
->uninstallModules(array(
'contact',
));
$result = $this
->runSensor('monitoring_installed_modules');
$this
->assertTrue($result
->isCritical());
$this
->assertEqual($result
->getMessage(), '1 modules delta, expected 0, Following modules are expected to be installed: Contact (contact)');
$this
->assertEqual($result
->getValue(), 1);
}
public function testEntityAggregator() {
$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(),
));
$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();
$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');
$sensor_config->settings['conditions'] = array(
'test' => array(
'field' => 'type',
'value' => $type2
->id(),
),
);
$sensor_config
->save();
$result = $this
->runSensor('entity_aggregate_test');
$this
->assertEqual($result
->getValue(), 2);
$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();
$term1 = $this
->createTerm($vocabulary);
$term2 = $this
->createTerm($vocabulary);
$node1 = $this
->drupalCreateNode(array(
'created' => \Drupal::time()
->getRequestTime(),
'type' => $type2
->id(),
'term_reference' => array(
array(
'target_id' => $term1
->id(),
),
),
));
$node2 = $this
->drupalCreateNode(array(
'created' => \Drupal::time()
->getRequestTime(),
'type' => $type2
->id(),
'term_reference' => array(
array(
'target_id' => $term1
->id(),
),
array(
'target_id' => $term2
->id(),
),
),
));
$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(),
),
),
));
$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');
$this
->assertEqual($result
->getValue(), 3);
$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_session = $this
->assertSession();
$assert_session
->elementTextContains('css', '#result', 'base_table');
$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'));
$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());
$this
->drupalPostForm(NULL, [
'settings[verbose_fields][2]' => 'revision_timestamp',
], t('Add another field'));
$this
->drupalPostForm(NULL, [
'conditions[1][field]' => 'nid',
'conditions[1][operator]' => '>',
'conditions[1][value]' => 4,
'settings[verbose_fields][3]' => 'test_wrong_field',
], t('Add another condition'));
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->clickLink('Entity Aggregate test');
$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'));
$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');
$this
->assertEqual($result
->getValue(), 1);
}
public function testPageNotFoundErrors() {
$test_user = $this
->drupalCreateUser([
'administer monitoring',
'monitoring reports',
'monitoring verbose',
]);
$this
->drupalLogin($test_user);
$event_time = \Drupal::time()
->getRequestTime();
Database::getConnection('default')
->insert('watchdog')
->fields([
'type' => 'page not found',
'message' => '@uri',
'variables' => serialize([
'%ip' => '127.0.0.1',
]),
'location' => 'http://example.com/non_existing_page',
'timestamp' => $event_time - 10,
])
->execute();
Database::getConnection('default')
->insert('watchdog')
->fields([
'type' => 'page not found',
'message' => '@uri',
'variables' => serialize([
'%ip' => '127.0.0.1',
]),
'location' => 'http://example.com/non_existing_page',
'timestamp' => $event_time,
])
->execute();
Database::getConnection('default')
->insert('watchdog')
->fields([
'type' => 'page not found',
'message' => '@uri',
'variables' => serialize([
'%ip' => '127.0.0.1',
]),
'location' => 'http://example.com/another_non_existing_page',
'timestamp' => $event_time - 10,
])
->execute();
$this
->drupalGet('admin/reports/monitoring/sensors/dblog_404');
$rows = $this
->getSession()
->getPage()
->findAll('css', '#unaggregated_result tbody tr');
$this
->assertEquals(2, count($rows), 'Two rows found.');
$this
->assertEquals('2', $rows[0]
->find('css', 'td:nth-child(2)')
->getText(), 'Two access to "/non_existing_page"');
$login_time = $rows[0]
->find('css', 'td:nth-child(3)')
->getText();
$expected_time = \Drupal::service('date.formatter')
->format($event_time, 'short');
$this
->assertEquals($expected_time, $login_time);
}
public function testTemporaryFilesUsages() {
$test_user = $this
->drupalCreateUser([
'administer site configuration',
'access site reports',
'administer monitoring',
'monitoring reports',
'monitoring verbose',
'monitoring force run',
]);
$this
->drupalLogin($test_user);
$result = $this
->runSensor('temporary_files_usages');
$this
->assertEqual($result
->getValue(), 0);
$this
->drupalPostForm('admin/reports/monitoring/sensors/temporary_files_usages', [], t('Run now'));
$this
->assertText('0 used temporary files');
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$node1 = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Example article 1',
]);
$node2 = $this
->drupalCreateNode([
'type' => 'article',
'title' => 'Example article 2',
]);
$file_usage = \Drupal::service('file.usage');
$file1 = File::create([
'fid' => 1,
'uuid' => 'aa',
'langcode' => 'en',
'uid' => 1,
'filename' => 'example_file_1',
'uri' => 'public://example_file_1',
'filemime' => 'example_mime',
'filesize' => 10,
'status' => 0,
'created' => time(),
'changed' => time(),
]);
$file_usage
->add($file1, 'monitoring_test', 'node', $node1
->id());
$file1
->setTemporary();
$file1
->save();
$file2 = File::create([
'fid' => 2,
'uuid' => 'bb',
'langcode' => 'en',
'uid' => 2,
'filename' => 'example_file_2',
'uri' => 'public://example_file_2',
'filemime' => 'example_mime',
'filesize' => 10,
'status' => 0,
'created' => time(),
'changed' => time(),
]);
$file_usage
->add($file2, 'monitoring_test', 'node', $node2
->id());
$file2
->setTemporary();
$file2
->save();
$file3 = File::create([
'fid' => 3,
'uuid' => 'cc',
'langcode' => 'en',
'uid' => 3,
'filename' => 'example_file_3',
'uri' => 'public://example_file_3',
'filemime' => 'example_mime',
'filesize' => 10,
'status' => 1,
'created' => time(),
'changed' => time(),
]);
$file_usage
->add($file3, 'monitoring', 'node', $node1
->id());
$file3
->save();
$this
->drupalPostForm('admin/reports/monitoring/sensors/temporary_files_usages', [], t('Run now'));
$result = $this
->runSensor('temporary_files_usages');
$this
->assertEqual($result
->getValue(), 2);
$this
->assertText('2 used temporary files');
$this
->assertLink('example_file_1');
$this
->assertLink('example_file_2');
$this
->assertLink($node1
->label());
$this
->assertLink($node2
->label());
$this
->assertLink('Make permanent');
$this
->clickLink('Make permanent');
$this
->assertText(t('File @file is now permanent.', [
'@file' => 'example_file_1',
]));
$this
->assertText('1 used temporary files');
$this
->assertLink('example_file_2');
$this
->assertLink($node2
->label());
$this
->assertNoLink('example_file_3');
$this
->assertNoLink('example_file_1');
$this
->assertNoLink($node1
->label());
}
protected function createVocabulary() {
$vocabulary = Vocabulary::create(array(
'vid' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
));
$vocabulary
->save();
return $vocabulary;
}
protected function createTerm($vocabulary) {
$term = Term::create(array(
'vid' => $vocabulary
->id(),
));
$term->name = $this
->randomMachineName();
$term->description = $this
->randomMachineName();
$term
->save();
return $term;
}
protected function loadWatchdog($type = 'monitoring') {
return \Drupal::database()
->query("SELECT * FROM {watchdog} WHERE type = :type", array(
':type' => $type,
))
->fetchAll();
}
}