You are here

protected function MonitoringCoreWebTest::doTestUserIntegritySensorPlugin in Monitoring 8

Tests the user integrity sensor.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\UserIntegritySensorPlugin

1 call to MonitoringCoreWebTest::doTestUserIntegritySensorPlugin()
MonitoringCoreWebTest::testSensors in tests/src/Functional/MonitoringCoreWebTest.php
Tests individual sensors.

File

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

Class

MonitoringCoreWebTest
Integration tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Functional

Code

protected function doTestUserIntegritySensorPlugin() {
  $test_user_first = $this
    ->drupalCreateUser(array(
    'administer monitoring',
  ), 'test_user');
  $this
    ->runSensor('user_integrity');

  // Delete the user and run the sensor.
  $test_user_first
    ->delete();
  $result = $this
    ->runSensor('user_integrity');
  $this
    ->assertTrue($result
    ->isOk());

  // Create the user again.
  $test_user_first = $this
    ->drupalCreateUser(array(
    'administer monitoring',
  ), 'test_user_1');
  $this
    ->drupalLogin($test_user_first);

  // Check sensor message after first privilege user creation.
  $result = $this
    ->runSensor('user_integrity');
  $this
    ->assertEqual($result
    ->getMessage(), '1 privileged user(s), 1 new user(s)');

  // Create second privileged user.
  $test_user_second = $this
    ->drupalCreateUser(array(), 'test_user_2', TRUE);
  $this
    ->drupalLogin($test_user_second);

  // Check sensor message after new privilege user creation.
  $result = $this
    ->runSensor('user_integrity');
  $this
    ->assertEqual($result
    ->getMessage(), '2 privileged user(s), 2 new user(s)');

  // Reset the user data, button is tested in UI tests.
  \Drupal::keyValue('monitoring.users')
    ->deleteAll();
  $result = $this
    ->runSensor('user_integrity');
  $this
    ->assertEqual($result
    ->getMessage(), '2 privileged user(s)');

  // Make changes to a user.
  $test_user_second
    ->setUsername('changed');
  $test_user_second
    ->save();

  // Check sensor message for user changes.
  $result = $this
    ->runSensor('user_integrity');
  $this
    ->assertEqual($result
    ->getMessage(), '2 privileged user(s), 1 changed user(s)');

  // Reset the user data again, check sensor message.
  \Drupal::keyValue('monitoring.users')
    ->deleteAll();
  $result = $this
    ->runSensor('user_integrity');
  $this
    ->assertEqual($result
    ->getMessage(), '2 privileged user(s)');

  // Add permissions to authenticated user with no privilege of registration.
  \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());

  // Count users included admin.
  $this
    ->assertEqual($result
    ->getMessage(), '3 privileged user(s), Privileged access for roles Authenticated user');

  // Add permissions to anonymous user and check the sensor.
  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');

  // Authenticated user with privilege of register.
  \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.');

  // Create an authenticated user and test that the sensor counter increments.
  $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();

  // Check sensor message for user changes.
  $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.');

  // Check sensor message with permissions revoked.
  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)');
}