You are here

class RulesDebugLoggerChannelTest in Rules 8.3

@coversDefaultClass \Drupal\rules\Logger\RulesDebugLoggerChannel @group Rules

Hierarchy

Expanded class hierarchy of RulesDebugLoggerChannelTest

File

tests/src/Unit/RulesDebugLoggerChannelTest.php, line 19

Namespace

Drupal\Tests\rules\Unit
View source
class RulesDebugLoggerChannelTest extends UnitTestCase {

  /**
   * The Drupal service container.
   *
   * @var \Drupal\Core\DependencyInjection\Container
   */
  protected $container;

  /**
   * The session service.
   *
   * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
   */
  protected $session;

  /**
   * The Rules logger.channel.rules_debug service.
   *
   * @var \Psr\Log\LoggerChannelInterface
   */
  protected $rulesDebugLogger;

  /**
   * The Rules logger.rules_debug_log service.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $rulesDebugLog;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $container = new ContainerBuilder();
    $this->rulesDebugLogger = $this
      ->prophesize(LoggerChannelInterface::class)
      ->reveal();
    $container
      ->set('logger.channel.rules_debug', $this->rulesDebugLogger);
    $this->session = new TestSession();
    $container
      ->set('session', $this->session);
    $this->rulesDebugLog = new RulesDebugLog($this->session);
    $container
      ->set('logger.rules_debug_log', $this->rulesDebugLog);
    \Drupal::setContainer($container);
    $this->container = $container;
  }

  /**
   * Tests LoggerChannel::log().
   *
   * @param string $psr3_message_level
   *   Expected PSR3 log level.
   * @param int $rfc_message_level
   *   Expected RFC 5424 log level.
   * @param bool $system_debug
   *   Is system debug logging enabled.
   * @param bool $debug_log_enabled
   *   Is debug logging enabled.
   * @param string $psr3_log_error_level
   *   Minimum required PSR3 log level at which to log.
   * @param int $expect_system_log
   *   Number of logs expected to be created.
   * @param int $expect_screen_log
   *   Number of messages expected to be created.
   * @param string $message
   *   Log message.
   *
   * @dataProvider providerTestLog
   *
   * @covers ::log
   */
  public function testLog($psr3_message_level, $rfc_message_level, $system_debug, $debug_log_enabled, $psr3_log_error_level, $expect_system_log, $expect_screen_log, $message) {

    // Clean up after previous test.
    $this->rulesDebugLog
      ->clearLogs();
    $config = $this
      ->getConfigFactoryStub([
      'rules.settings' => [
        'system_log' => [
          'log_level' => $psr3_log_error_level,
        ],
        'debug_log' => [
          'enabled' => $debug_log_enabled,
          'system_debug' => $system_debug,
          'log_level' => $psr3_log_error_level,
        ],
      ],
    ]);
    $channel = new RulesDebugLoggerChannel($this->rulesDebugLog, $config);
    $addedLogger = $this
      ->prophesize(LoggerInterface::class);
    $addedLogger
      ->log($rfc_message_level, $message, Argument::type('array'))
      ->shouldBeCalledTimes($expect_screen_log);
    $channel
      ->addLogger($addedLogger
      ->reveal());
    $channel
      ->log($psr3_message_level, $message, []);
    $messages = $this->rulesDebugLog
      ->getLogs();
    if ($expect_screen_log > 0) {
      $this
        ->assertNotNull($messages);
      $context = [
        'channel' => 'rules_debug',
        'link' => '',
        'element' => NULL,
        'scope' => NULL,
        'path' => NULL,
      ];
      $context += $messages[0]['context'];
      $this
        ->assertEquals([
        0 => [
          'message' => $message,
          'context' => $context,
          'level' => $psr3_message_level,
          'timestamp' => $context['timestamp'],
          'scope' => NULL,
          'path' => NULL,
        ],
      ], $messages, "actual =" . var_export($messages, TRUE));
    }
    else {
      $this
        ->assertCount(0, $messages);
    }
  }

  /**
   * Data provider for self::testLog().
   */
  public function providerTestLog() {
    return [
      [
        'psr3_message_level' => LogLevel::DEBUG,
        'rfc_message_level' => RfcLogLevel::DEBUG,
        'system_debug_enabled' => FALSE,
        'debug_log_enabled' => FALSE,
        'min_psr3_level' => LogLevel::DEBUG,
        'expected_system_logs' => 0,
        'expected_screen_logs' => 0,
        'message' => 'apple',
      ],
      [
        'psr3_message_level' => LogLevel::DEBUG,
        'rfc_message_level' => RfcLogLevel::DEBUG,
        'system_debug_enabled' => FALSE,
        'debug_log_enabled' => TRUE,
        'min_psr3_level' => LogLevel::DEBUG,
        'expected_system_logs' => 0,
        'expected_screen_logs' => 1,
        'message' => 'pear',
      ],
      [
        'psr3_message_level' => LogLevel::CRITICAL,
        'rfc_message_level' => RfcLogLevel::CRITICAL,
        'system_debug_enabled' => TRUE,
        'debug_log_enabled' => FALSE,
        'min_psr3_level' => LogLevel::DEBUG,
        'expected_system_logs' => 1,
        'expected_screen_logs' => 0,
        'message' => 'banana',
      ],
      [
        'psr3_message_level' => LogLevel::CRITICAL,
        'rfc_message_level' => RfcLogLevel::CRITICAL,
        'system_debug_enabled' => TRUE,
        'debug_log_enabled' => TRUE,
        'min_psr3_level' => LogLevel::DEBUG,
        'expected_system_logs' => 1,
        'expected_screen_logs' => 1,
        'message' => 'carrot',
      ],
      [
        'psr3_message_level' => LogLevel::CRITICAL,
        'rfc_message_level' => RfcLogLevel::CRITICAL,
        'system_debug_enabled' => TRUE,
        'debug_log_enabled' => FALSE,
        'min_psr3_level' => LogLevel::DEBUG,
        'expected_system_logs' => 1,
        'expected_screen_logs' => 0,
        'message' => 'orange',
      ],
      [
        'psr3_message_level' => LogLevel::CRITICAL,
        'rfc_message_level' => RfcLogLevel::CRITICAL,
        'system_debug_enabled' => TRUE,
        'debug_log_enabled' => TRUE,
        'min_psr3_level' => LogLevel::DEBUG,
        'expected_system_logs' => 1,
        'expected_screen_logs' => 1,
        'message' => 'kumquat',
      ],
      [
        'psr3_message_level' => LogLevel::INFO,
        'rfc_message_level' => RfcLogLevel::INFO,
        'system_debug_enabled' => TRUE,
        'debug_log_enabled' => FALSE,
        'min_psr3_level' => LogLevel::CRITICAL,
        'expected_system_logs' => 0,
        'expected_screen_logs' => 0,
        'message' => 'cucumber',
      ],
      [
        'psr3_message_level' => LogLevel::INFO,
        'rfc_message_level' => RfcLogLevel::INFO,
        'system_debug_enabled' => TRUE,
        'debug_log_enabled' => TRUE,
        'min_psr3_level' => LogLevel::CRITICAL,
        'expected_system_logs' => 0,
        'expected_screen_logs' => 0,
        'message' => 'dragonfruit',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RulesDebugLoggerChannelTest::$container protected property The Drupal service container.
RulesDebugLoggerChannelTest::$rulesDebugLog protected property The Rules logger.rules_debug_log service.
RulesDebugLoggerChannelTest::$rulesDebugLogger protected property The Rules logger.channel.rules_debug service.
RulesDebugLoggerChannelTest::$session protected property The session service.
RulesDebugLoggerChannelTest::providerTestLog public function Data provider for self::testLog().
RulesDebugLoggerChannelTest::setUp protected function Overrides UnitTestCase::setUp
RulesDebugLoggerChannelTest::testLog public function Tests LoggerChannel::log().
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.