Convert static functions in Content moderation state Entity class to Service for easier Unit testing

Created on 1 September 2022, over 2 years ago
Updated 11 December 2023, about 1 year ago

Problem/Motivation

Now the Drupal\content_moderation\Entity\ContentModerationState class contains two static functions:

  public static function updateOrCreateFromEntity(ContentModerationState $content_moderation_state) {}
  public static function loadFromModeratedEntity(EntityInterface $entity) {}

They works well. But when we use those functions in our custom code and want to cover the code by unit tests - it's very hard to mock the logic of that functions!

And seems those functions have no direct dependencies to the ContentModerationState entity class.

Steps to reproduce

1. Create a function that gets a moderation state label from a moderated content entity, like this:

  protected function getModerationStatusLabel(EntityInterface $entity) {
    return \Drupal\content_moderation\Entity\ContentModerationState::loadFromModeratedEntity($entity)
      ->workflow
      ->entity
      ->get('type_settings')['states'][$entity->moderation_state->value]['label'];
  }

2. Try to quickly write a Unit test for this function :)

Proposed resolution

To make the developers life easier, will be much better to put these functions in a separate class and make them non-static.

Also will be good to make this class available as a service, to give DI abilities.

And we can keep current static functions in the class as "proxy", to not break the currently working code, that just calls the new service.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
Content moderationΒ  β†’

Last updated 11 days ago

  • Maintained by
  • πŸ‡¦πŸ‡ΊAustralia @Sam152
Created by

πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024