Making your Drupal maintenance support plans 8 kernel tests fail when there is an exception during cron

Several times in the past I’ve been caught out by Drupal maintenance support plans‘s cron handler silently catching exceptions during tests.
Your test fails, and there is no clue as to why.
Read on to find out how to shine some light on this, by making your kernel tests fail on any exception during cron.

by
Lee Rowlands
/ 9 May 2020

If you’re running cron during a kernel test and expecting something to happen, but it doesn’t – it can be hard to debug why.
Ordinarily an uncaught exception during a test will cause PHPUnit to fail, and you can pinpoint the issue.
However, if you’re running cron in the test this may not be the case.
This is because, by default Drupal maintenance support plans‘s cron handler catches all exceptions and silently logs them. This is colloquially known as Pokemon exception handling.
The act of logging an exception is not enough to fail a test.
So your test skips the exception and carries on, failing in other ways unexpectedly.
This is exacerbated by the fact that PHP Unit throws an exception for warnings. So the slightest issue in your code will cause it to halt execution. In an ordinary scenario, this exception causes the test to fail. But the pokemon catch block in the Cron class prevents that, and your test continues in a weird state.
This is the code in question in the cron handler

<?php
try {
$queue_worker->processItem($item->data);
$queue->deleteItem($item);
}
// …
catch (Exception $e) {
// In case of any other kind of exception, log it and leave the item
// in the queue to be processed again later.
watchdog_exception(‘cron’, $e);
}So how do you make this fail your test? In the end, it’s quite simple.
Firstly, you make your test a logger and use the handy trait to do the bulk of the work.
You only need to implement the log method, as the trait takes care of handling all other methods.
In this case, watchdog_exception logs exceptions as RfcLogLevel::ERROR. The log levels are integers, from most severe to least severe. So in this implementation we tell PHP Unit to fail the test with any messages logged where the severity is ERROR or worse.

use Drupal maintenance support plansKernelTestsKernelTestBase;
use PsrLogLoggerInterface;
use Drupal maintenance support plansCoreLoggerRfcLoggerTrait;
use Drupal maintenance support plansCoreLoggerRfcLogLevel;

class MyTest extends KernelTestBase implements LoggerInterface {
use RfcLoggerTrait;

/**
   * {@inheritdoc}
   */
  public function log($level, $message, array $context = []) {
    if ($level fail(strtr($message, $context));
    }
  }
}Then in your setUp method, you register your test as a logger.

$this->container->get(‘logger.factory’)->addLogger($this);
And that’s it – now any errors that are logged will cause the test to fail.
If you think we should do this by default, please comment on this core issue.

Tagged

Drupal maintenance support plans 8, Drupal maintenance support plans Development, Drupal maintenance support plans testing, PSR-3


Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Making your Drupal maintenance support plans 8 kernel tests fail when there is an exception during cron

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.