Problem/Motivation

Media entity used to provide custom validation API to be used by type plugins. In #2529178: Convert media type validation code to a constraint we converted this feature to core's Constraint API. All media type providers need to follow that change.

Proposed resolution

Media entity slideshow should follow this change too. Media entity Twitter already did that in #2578927: [Chase media_entity] Convert validation to Constraints API and can be used as a reference.

Comments

slashrsm created an issue. See original summary.

sanja_m’s picture

Version: » 8.x-1.x-dev
Assigned: Unassigned » sanja_m

Working on this.

sanja_m’s picture

Status: Active » Needs review
StatusFileSize
new6.5 KB

Added ItemsCount constraint.

slashrsm’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/Validation/Constraint/ItemsCountConstraint.php
    @@ -0,0 +1,34 @@
    +  public function __construct($options = null) {
    +    parent::__construct($options);
    +  }
    

    We don't need to override constructor.

  2. +++ b/src/Tests/ConstraintsTest.php
    @@ -0,0 +1,84 @@
    +class ConstraintsTest extends WebTestBase {
    

    It would be nicer to have unit test for this.

sanja_m’s picture

Status: Needs work » Needs review
StatusFileSize
new7.32 KB
new5.77 KB

#4-1. and 2. fixed.

slashrsm’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/Validation/Constraint/ItemsCountConstraintValidator.php
    @@ -0,0 +1,31 @@
    diff --git a/src/Tests/ConstraintsTest.php b/src/Tests/ConstraintsTest.php
    
    diff --git a/src/Tests/ConstraintsTest.php b/src/Tests/ConstraintsTest.php
    new file mode 100644
    
    new file mode 100644
    index 0000000..17033b3
    
    index 0000000..17033b3
    --- /dev/null
    
    --- /dev/null
    +++ b/src/Tests/ConstraintsTest.php
    
    +++ b/src/Tests/ConstraintsTest.php
    +++ b/src/Tests/ConstraintsTest.php
    @@ -0,0 +1,122 @@
    
    @@ -0,0 +1,122 @@
    +<?php
    +
    +/**
    + * @file
    + * Contains \Drupal\media_entity_slideshow\Tests\ConstraintsTest.
    + */
    +
    +namespace Drupal\media_entity_slideshow\Tests;
    +
    +use Drupal\media_entity_slideshow\Plugin\Validation\Constraint\ItemsCountConstraint;
    +use Drupal\media_entity_slideshow\Plugin\Validation\Constraint\ItemsCountConstraintValidator;
    +use Drupal\Tests\UnitTestCase;
    +
    +/**
    + * Tests media_entity_slideshow constraints.
    + *
    + * @group media_entity
    + */
    

    PHPUnit tests are usually located in module_root/tests/src and namespaced \Drupal\Tests\module_name\...

  2. +++ b/src/Tests/ConstraintsTest.php
    @@ -0,0 +1,122 @@
    +  /**
    +   * Provides test data for testValidation().
    +   */
    +  public function dataProvider() {
    +    return [
    +      'invalid data' => ['', 1],
    +      'valid data' => ['Some text', 0]
    +    ];
    +  }
    

    I think that we don't need data provider since there will always be just two options.

  3. +++ b/src/Tests/ConstraintsTest.php
    @@ -0,0 +1,122 @@
    +  /**
    

    Leave an empty line at the beginning and end of the class definition.

  4. +++ b/src/Tests/ConstraintsTest.php
    @@ -0,0 +1,122 @@
    +    $this->property = $value == '' ? NULL : $value;
    

    Let's make argument optional and default it to NULL. This way it won't be needed to do empty string check any more.

sanja_m’s picture

Status: Needs work » Needs review
StatusFileSize
new7.44 KB
new6.97 KB

#6 - 1, 2, 3 and 4 fixed.

slashrsm’s picture

Status: Needs review » Fixed

Committed. Thank you!

  • slashrsm committed 5d79323 on 8.x-1.x authored by sanja_m
    Issue #2578945 by sanja_m, slashrsm: [Chase media_entity] Convert...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.