You are here

RavenTest.php in Raven: Sentry Integration 3.x

File

tests/src/Functional/RavenTest.php
View source
<?php

namespace Drupal\Tests\raven\Functional;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;

/**
 * Tests Raven module.
 *
 * @group raven
 */
class RavenTest extends BrowserTestBase {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Modules to install.
   *
   * @var array
   */
  protected static $modules = [
    'raven',
    'raven_test',
  ];

  /**
   * Tests Raven module configuration UI and hooks.
   */
  public function testRavenConfigAndHooks() {
    $admin_user = $this
      ->drupalCreateUser([
      'administer site configuration',
    ]);
    $this
      ->drupalLogin($admin_user);
    $config['raven[php][client_key]'] = 'https://user@sentry.test/123456';
    $config['raven[php][fatal_error_handler]'] = 1;
    foreach (range(1, 8) as $level) {
      $config["raven[php][log_levels][{$level}]"] = '1';
    }
    $this
      ->drupalGet('admin/config/development/logging');
    $this
      ->submitForm($config, $this
      ->t('Save configuration'));
    $this
      ->assertSession()
      ->responseHeaderEquals('X-Logged', 'Logged');
    $this
      ->assertSession()
      ->responseHeaderEquals('X-Not-Logged', NULL);

    // @todo Temporary work-around to support Drupal 9.2 and lower.
    $path = $this->container
      ->has('extension.path.resolver') ? $this->container
      ->get('extension.path.resolver')
      ->getPath('module', 'raven_test') : drupal_get_path('module', 'raven_test');
    $this
      ->assertSession()
      ->responseHeaderEquals('X-Stacktrace-File', "/{$path}/raven_test.module");

    // Test fatal error handling.
    $memory_limit = mt_rand(16000000, 17999999);
    $url = $admin_user
      ->toUrl()
      ->setOption('query', [
      'memory_limit' => $memory_limit,
    ]);

    // Output should be the memory limit and 0 pending events/requests.
    $this
      ->assertEquals($memory_limit, $this
      ->drupalGet($url));

    // Test ignored channels.
    $config = [
      'raven[php][ignored_channels]' => "X-Logged\r\n",
    ];
    $this
      ->drupalGet('admin/config/development/logging');
    $this
      ->submitForm($config, $this
      ->t('Save configuration'));
    $this
      ->assertSession()
      ->responseHeaderEquals('X-Logged', NULL);

    // Test client functionality after logger is serialized and unserialized.
    unserialize(serialize($this->container
      ->get('logger.raven')));
    \Sentry\captureException(new \Exception('This is a test.'));
  }

}

Classes

Namesort descending Description
RavenTest Tests Raven module.