You are here

public function MonitoringCoreKernelTest::testImageMissingStyleSensorPlugin in Monitoring 8

Tests dblog missing image style sensor.

Logged through watchdog.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\ImageMissingStyleSensorPlugin

File

tests/src/Kernel/MonitoringCoreKernelTest.php, line 283

Class

MonitoringCoreKernelTest
Kernel tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Kernel

Code

public function testImageMissingStyleSensorPlugin() {
  $this
    ->installSchema('file', [
    'file_usage',
  ]);
  $this
    ->installEntitySchema('file');
  $this
    ->installConfig([
    'system',
  ]);

  // Fake some image style derivative errors.
  $file = file_save_data($this
    ->randomMachineName());

  /** @var \Drupal\file\FileUsage\FileUsageInterface $usage */
  $usage = \Drupal::service('file.usage');
  $usage
    ->add($file, 'monitoring_test', 'node', 123456789);
  for ($i = 1; $i <= 6; $i++) {

    // We use the logger.dblog service to be able to set the referer.
    \Drupal::service('logger.dblog')
      ->notice('Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', [
      '%source_image_path' => $file
        ->getFileUri(),
      '%derivative_path' => 'hash://styles/preview/1234.jpeg',
      'request_uri' => '',
      'uid' => 0,
      'channel' => 'image',
      'link' => '',
      'referer' => 'http://example.com/node/123456789',
      'ip' => '127.0.0.1',
      'timestamp' => \Drupal::time()
        ->getRequestTime() - $i,
    ]);
  }
  \Drupal::logger('image')
    ->notice('Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', array(
    '%source_image_path' => 'public://portrait-pictures/bluemouse.jpeg',
    '%derivative_path' => 'hash://styles/preview/5678.jpeg',
    'timestamp' => \Drupal::time()
      ->getRequestTime(),
  ));

  // Run sensor and test the output.
  $result = $this
    ->runSensor('dblog_image_missing_style');
  $this
    ->assertEquals(6, $result
    ->getValue());
  $this
    ->assertTrue(strpos($result
    ->getMessage(), $file
    ->getFileUri()) !== FALSE);
  $this
    ->assertTrue($result
    ->isWarning());
  $verbose_output = $result
    ->getVerboseOutput();
  $this
    ->setRawContent(\Drupal::service('renderer')
    ->renderPlain($verbose_output));
  $xpath = $this
    ->xpath('//*[@id="unaggregated_result"]/div/table/tbody');
  $this
    ->assertEquals('public://portrait-pictures/bluemouse.jpeg', (string) $xpath[0]->tr[0]->td[2]);
  $this
    ->assertEquals('http://example.com/node/123456789', (string) $xpath[0]->tr[6]->td[1]);
  $this
    ->assertLink('7');
}