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 Instagram 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.

sdstyles’s picture

Status: Active » Needs review
StatusFileSize
new2.84 KB
slashrsm’s picture

Status: Needs review » Needs work

Thank you. I can one comment:

+++ b/src/Plugin/MediaEntity/Type/Instagram.php
@@ -229,12 +229,18 @@ class Instagram extends MediaTypeBase {
 
     // Validate regex.
     if (!$matches) {
-      throw new MediaTypeException($this->configuration['source_field'], 'Not valid URL/embed code.');
+      $source_field_name = $this->configuration['source_field'];
+      foreach ($media->get($source_field_name) as &$embed_code) {

The actual check should be done in constraint validator. attachConstraints() is only adding them without assuming what result of the check will be.

See how we did it in media_entity_twitter: https://github.com/drupal-media/media_entity_twitter/pull/9/files

Would also be great to have tests coverage for constraints (Twitter pull request implemented that too and can be used as a reference).

sdstyles’s picture

Status: Needs work » Needs review
StatusFileSize
new9.02 KB

Changed, also added tests coverage as on Twitter pull request.

slashrsm’s picture

Great. Looks almost ready to go. Just few nit-piks:

  1. +++ b/.travis.yml
    @@ -0,0 +1,50 @@
    +  - ./vendor/bin/phpunit -c core modules/media_entity_twitter/tests/src/Unit
    

    s/media_entity_twitter/media_entity_instagram

  2. +++ b/src/Plugin/MediaEntity/Type/Instagram.php
    @@ -249,7 +251,7 @@ class Instagram extends MediaTypeBase {
        *
        * @see preg_match()
        */
    -  protected function matchRegexp(MediaInterface $media) {
    +  public function matchRegexp(MediaInterface $media) {
         $matches = array();
         $source_field = $this->configuration['source_field'];
     
    

    This should be static if we want to use it as constraint validator currently does. We took a different approach with Twitter and exposed only regexp.

  3. +++ b/tests/src/Unit/ConstraintsTest.php
    @@ -0,0 +1,67 @@
    +      'invalid URL' => ['https://instagram.com/pp/2hi3DVDgsN/', 1],
    +      'invalid ID' => ['https://instagram.com/pp/2hi3DVDgsNtYpO/', 1],
    +    ];
    

    Both of this seem to be wrong URL. ID parts seems OK in both cases.

sdstyles’s picture

StatusFileSize
new9.14 KB

Exposed $validationRegexp property in constraint validator.

slashrsm’s picture

Thanks! Created pull request on GitHub to trigger Travis build. Will merge when it confirms it is green.

https://github.com/drupal-media/media_entity_instagram/pull/10

@sdstyles: We usually attach inderdiffs to update patches. See documentation for more info: https://www.drupal.org/documentation/git/interdiff

slashrsm’s picture

Status: Needs review » Needs work

It failed :(

https://travis-ci.org/drupal-media/media_entity_instagram/jobs/84265418

+++ b/src/Validation/Constraint/InstagramEmbedCodeConstraint.php
@@ -0,0 +1,30 @@
+namespace Drupal\media_entity_instagram\Plugin\Validation\Constraint;

Namespace is wrong. Same with constraint validator.

sdstyles’s picture

Status: Needs work » Needs review
StatusFileSize
new9.16 KB

Changed namespace in InstagramEmbedCodeConstraintValidator.php to
Drupal\media_entity_instagram\Plugin\Validation\ConstraintValidator;

slashrsm’s picture

Status: Needs review » Needs work

Same problem with InstagramEmbedCodeConstraint itself. You can try to run test locally to see if it passes. It should run pretty quick.

sdstyles’s picture

Status: Needs work » Needs review

Hi @slashrsm, I've created new pull request where I fixed namespaces and another small issues, please review it.
https://github.com/drupal-media/media_entity_instagram/pull/11

slashrsm’s picture

Status: Needs review » Fixed

Merged. Thanks!

  • sdstyles committed 5c04f77 on 8.x-1.x
    Issue #2578935 by sdstyles: [Chase media_entity] Convert validation to...

Status: Fixed » Closed (fixed)

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