0d593449c650e2481bdbd4d855766d7333eac0d1
[yaffs-website] / web / modules / contrib / media_entity_instagram / tests / src / Unit / ConstraintsTest.php
1 <?php
2
3 namespace Drupal\Tests\media_entity_instagram\Unit;
4
5 use Drupal\Core\Field\Plugin\Field\FieldType\StringLongItem;
6 use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
7 use Drupal\media_entity_instagram\Plugin\Validation\Constraint\InstagramEmbedCodeConstraint;
8 use Drupal\media_entity_instagram\Plugin\Validation\Constraint\InstagramEmbedCodeConstraintValidator;
9 use Drupal\Tests\UnitTestCase;
10
11 /**
12  * Tests media_entity_instagram constrains.
13  *
14  * @group media
15  */
16 class ConstraintsTest extends UnitTestCase {
17
18   /**
19    * Tests InstagramEmbedCode constraints.
20    *
21    * @covers \Drupal\media_entity_instagram\Plugin\Validation\Constraint\InstagramEmbedCodeConstraint
22    * @covers \Drupal\media_entity_instagram\Plugin\Validation\Constraint\InstagramEmbedCodeConstraintValidator
23    *
24    * @dataProvider embedCodeProvider
25    */
26   public function testInstagramEmbedCodeConstraint($embed_code, $expected_violation_count) {
27     // Check message in constraint.
28     $constraint = new InstagramEmbedCodeConstraint();
29     $this->assertEquals(addslashes('Not valid Instagram URL/Embed code.'), addslashes($constraint->message), 'Correct constraint message found.');
30
31     $execution_context = $this->getMockBuilder('\Drupal\Core\TypedData\Validation\ExecutionContext')
32       ->disableOriginalConstructor()
33       ->getMock();
34
35     if ($expected_violation_count) {
36       $execution_context->expects($this->exactly($expected_violation_count))
37         ->method('addViolation')
38         ->with($constraint->message);
39     }
40     else {
41       $execution_context->expects($this->exactly($expected_violation_count))
42         ->method('addViolation');
43     }
44
45     $validator = new InstagramEmbedCodeConstraintValidator();
46     $validator->initialize($execution_context);
47
48     $definition = $this->getMock(ComplexDataDefinitionInterface::class);
49     $definition->method('getPropertyDefinitions')->willReturn([]);
50
51     $data = new StringLongItem($definition);
52     $data->set('value', $embed_code);
53     $validator->validate($data, $constraint);
54   }
55
56   /**
57    * Provided test data for testInstagramEmbedCodeConstraint().
58    */
59   public function embedCodeProvider() {
60     return [
61       'valid URL' => ['https://instagram.com/p/2hi3DVDgsN/', 0],
62       'valid embed code' => ['<blockquote class="instagram-media" data-instgrm-captioned data-instgrm-version="5" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);"><div style="padding:8px;"> <div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding:50% 0; text-align:center; width:100%;"> <div style=" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAAGFBMVEUiIiI9PT0eHh4gIB4hIBkcHBwcHBwcHBydr+JQAAAACHRSTlMABA4YHyQsM5jtaMwAAADfSURBVDjL7ZVBEgMhCAQBAf//42xcNbpAqakcM0ftUmFAAIBE81IqBJdS3lS6zs3bIpB9WED3YYXFPmHRfT8sgyrCP1x8uEUxLMzNWElFOYCV6mHWWwMzdPEKHlhLw7NWJqkHc4uIZphavDzA2JPzUDsBZziNae2S6owH8xPmX8G7zzgKEOPUoYHvGz1TBCxMkd3kwNVbU0gKHkx+iZILf77IofhrY1nYFnB/lQPb79drWOyJVa/DAvg9B/rLB4cC+Nqgdz/TvBbBnr6GBReqn/nRmDgaQEej7WhonozjF+Y2I/fZou/qAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;"></div></div> <p style=" margin:8px 0 0 0; padding:0 4px;"> <a href="https://instagram.com/p/2hi3DVDgsN/" style=" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;" target="_blank">Once upon a time there were 10 mysterious golden tickets.... #drupalcon</a></p> <p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;">A photo posted by DrupalCon (@drupalcon) on <time style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;" datetime="2015-05-11T02:01:51+00:00">May 10, 2015 at 7:01pm PDT</time></p></div></blockquote><script async defer src="//platform.instagram.com/en_US/embeds.js"></script>', 0],
63       'invalid URL' => ['https://instagram.com/ppp/2hi3DVDgsN/', 1],
64     ];
65   }
66
67 }