Commit 711cb37e authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3261305 by jurgenhaas: Add an action to write a log message

parent 985947e0
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  module:
    - eca_content
    - eca_log
id: process_shjfq8e
modeller: bpmn_io
label: 'Write Log Message'
version: 1.0.0
weight: null
events:
  Event_1eshshx:
    plugin: 'content_entity:presave'
    label: 'Presave node'
    fields:
      -
        key: type
        value: 'node _all'
    successors:
      -
        id: Activity_02f0tls
        condition: ''
conditions: {  }
gateways:
  '':
    type: 4
    successors: {  }
actions:
  Activity_02f0tls:
    plugin: eca_write_log_message
    label: Log
    fields:
      -
        key: channel
        value: eca
      -
        key: severity
        value: '6'
      -
        key: message
        value: 'Node "[entity:title]" is about to be saved'
    successors: {  }
+59 −0
Original line number Diff line number Diff line
langcode: en
status: true
dependencies:
  config:
    - eca.eca.process_shjfq8e
id: process_shjfq8e
filename: ''
modeldata: |
  <?xml version="1.0" encoding="UTF-8"?>
  <bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:qa="http://some-company/schema/bpmn/qa" id="sample-diagram" targetNamespace="http://bpmn.io/schema/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
    <bpmn2:process id="Process_sHJFQ8E" name="Write Log Message" isExecutable="true" camunda:versionTag="1.0.0">
      <bpmn2:startEvent id="Event_1eshshx" name="Presave article node" camunda:modelerTemplate="org.drupal.content_entity:presave">
        <bpmn2:extensionElements>
          <camunda:properties>
            <camunda:property name="pluginid" value="content_entity:presave" />
          </camunda:properties>
          <camunda:field name="type">
            <camunda:string>node article</camunda:string>
          </camunda:field>
        </bpmn2:extensionElements>
        <bpmn2:outgoing>Flow_0bcpf87</bpmn2:outgoing>
      </bpmn2:startEvent>
      <bpmn2:task id="Activity_02f0tls" name="Log" camunda:modelerTemplate="org.drupal.eca_write_log_message">
        <bpmn2:extensionElements>
          <camunda:properties>
            <camunda:property name="pluginid" value="eca_write_log_message" />
          </camunda:properties>
          <camunda:field name="channel">
            <camunda:string>eca</camunda:string>
          </camunda:field>
          <camunda:field name="severity">
            <camunda:string>6</camunda:string>
          </camunda:field>
          <camunda:field name="message">
            <camunda:string>Node [entity:title] is about to be saved</camunda:string>
          </camunda:field>
        </bpmn2:extensionElements>
        <bpmn2:incoming>Flow_0bcpf87</bpmn2:incoming>
      </bpmn2:task>
      <bpmn2:sequenceFlow id="Flow_0bcpf87" sourceRef="Event_1eshshx" targetRef="Activity_02f0tls" />
    </bpmn2:process>
    <bpmndi:BPMNDiagram id="sid-1cabc76d-d88d-4356-9904-af226630ab20">
      <bpmndi:BPMNPlane id="sid-46a4a3ff-1826-420e-b0f0-4666d0e322a8" bpmnElement="Process_sHJFQ8E">
        <bpmndi:BPMNEdge id="Flow_0bcpf87_di" bpmnElement="Flow_0bcpf87">
          <di:waypoint x="328" y="120" />
          <di:waypoint x="380" y="120" />
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNShape id="Event_1eshshx_di" bpmnElement="Event_1eshshx">
          <dc:Bounds x="292" y="102" width="36" height="36" />
          <bpmndi:BPMNLabel>
            <dc:Bounds x="275" y="145" width="73" height="27" />
          </bpmndi:BPMNLabel>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape id="Activity_02f0tls_di" bpmnElement="Activity_02f0tls">
          <dc:Bounds x="380" y="80" width="100" height="80" />
        </bpmndi:BPMNShape>
      </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
  </bpmn2:definitions>
+8 −0
Original line number Diff line number Diff line
name: 'ECA model test: Logging'
type: module
description: 'Support module for the ECA model tests.'
package: Testing

config_devel:
  - eca.eca.process_shjfq8e
  - eca.model.process_shjfq8e
+71 −4
Original line number Diff line number Diff line
@@ -89,14 +89,25 @@ abstract class Base extends KernelTestBase {
  }

  /**
   * Verify that noe error or worse has been logged.
   * Verify that no error or worse has been logged.
   *
   * Optionally this can also assert a number of expected log records, that
   * need to be present and won't be treated as available errors.
   *
   * @param \Drupal\Tests\eca\Kernel\Model\LogRecord[] $logRecords
   */
  protected function assertNoError(): void {
  protected function assertNoError(array $logRecords = []): void {
    foreach ($this->container->get(self::$testLogServiceName)->cleanLogs() as $log_message) {
      // Avoid deprecation messages from \Drupal\Component\Render\FormattableMarkup::placeholderFormat.
      unset($log_message[2]['channel'], $log_message[2]['link'], $log_message[2]['uid'], $log_message[2]['request_uri'], $log_message[2]['referer'], $log_message[2]['ip'], $log_message[2]['timestamp']);
      foreach ($logRecords as $index => $logRecord) {
        if ($logRecord->compare($log_message[0], $log_message[2]['channel'], $log_message[1], $log_message[2])) {
          $this->assertNotNull('record exists', $logRecord->__toString());
          unset($logRecords[$index]);
          continue 2;
        }
      }
      $this->assertGreaterThan(RfcLogLevel::ERROR, $log_message[0], strip_tags(t($log_message[1], $log_message[2])));
    }
    self::assertEmpty($logRecords, 'Expected log records missing: ' . PHP_EOL . implode(PHP_EOL, $logRecords));
  }

  /**
@@ -156,3 +167,59 @@ abstract class Base extends KernelTestBase {
  }

}

class LogRecord {

  private int $severity;

  private string $channel;

  private string $message;

  private array $arguments;

  /**
   * @param int $severity
   * @param string $channel
   * @param string $message
   * @param array $arguments
   */
  public function __construct(int $severity, string $channel, string $message, array $arguments = []) {
    $this->severity = $severity;
    $this->channel = $channel;
    $this->message = $message;
    $this->arguments = $arguments;
  }

  /**
   * @param string $message
   * @param array $arguments
   *
   * @return string
   */
  public static function format(string $message, array $arguments = []): string {
    return strip_tags(t($message, $arguments));
  }

  /**
   * @param int $severity
   * @param string $channel
   * @param string $message
   * @param array $arguments
   *
   * @return bool
   */
  public function compare(int $severity, string $channel, string $message, array $arguments = []): bool {
    return $this->severity === $severity &&
      $this->channel === $channel &&
      $this->__toString() === self::format($message, $arguments);
  }

  /**
   * @return string
   */
  public function __toString(): string {
    return self::format($this->message, $this->arguments);
  }

}
+55 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\eca\Kernel\Model;

use Drupal\Core\Logger\RfcLogLevel;
use Drupal\node\Entity\Node;

/**
 * Model test for logging.
 *
 * @group eca_model
 */
class LoggingTest extends Base {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'eca_content',
    'eca_log',
    'eca_test_model_logging',
  ];

  /**
   * {@inheritdoc}
   */
  public function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('node');
    $this->installSchema('node', ['node_access']);
    $this->switchUser(1);
  }

  /**
   * Tests logging on creating/saving an article.
   */
  public function testArticle(): void {
    $title = $this->randomMachineName();
    /** @var \Drupal\node\NodeInterface $node */
    $node = Node::create([
      'type' => 'article',
      'tnid' => 0,
      'uid' => 1,
      'title' => $title,
    ]);
    $node->save();

    $this->assertNoMessages();
    $this->assertNoError([
      new LogRecord(RfcLogLevel::INFO, 'eca', 'Node "@label" is about to be saved', ['@label' => $title]),
    ]);
  }

}