You are here

protected function MonitoringCoreWebTest::doTestPhpNoticesSensor in Monitoring 8

Tests the user integrity sensor.

See also

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

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

File

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

Class

MonitoringCoreWebTest
Integration tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Functional

Code

protected function doTestPhpNoticesSensor() {
  $test_user_first = $this
    ->drupalCreateUser(array(
    'administer monitoring',
    'monitoring reports',
    'monitoring verbose',
  ), 'test_user_php');
  $this
    ->drupalLogin($test_user_first);

  // Prepare a fake PHP error.
  $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,
  ];

  // Prepare another fake PHP notice.
  $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,
  ];

  // Log them.
  \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();
  };

  // Check out sensor result page.
  $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');

  // Test Filename shortening.
  $this
    ->assertEqual(str_replace(DRUPAL_ROOT . '/', '', $error['%file'] . ':' . $error['%line']), $first_message[4], 'Filename was successfully shortened.');
}