You are here

class ConstraintsTest in Media entity Twitter 8

Tests media_entity_twitter constraints.

@group media_entity

Hierarchy

Expanded class hierarchy of ConstraintsTest

File

tests/src/Unit/ConstraintsTest.php, line 18

Namespace

Drupal\Tests\media_entity_twitter\Unit
View source
class ConstraintsTest extends UnitTestCase {

  /**
   * Creates a string_long FieldItemInterface wrapper around a value.
   *
   * @param string $value
   *   The wrapped value.
   *
   * @return \Drupal\Core\Field\FieldItemInterface
   *   Mocked string field item.
   */
  protected function getMockFieldItem($value) {
    $definition = $this
      ->prophesize(ComplexDataDefinitionInterface::class);
    $definition
      ->getPropertyDefinitions()
      ->willReturn([]);
    $item = new StringLongItem($definition
      ->reveal());
    $item
      ->set('value', $value);
    return $item;
  }

  /**
   * Tests TweetEmbedCode constraint.
   *
   * @covers \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetEmbedCodeConstraintValidator
   * @covers \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetEmbedCodeConstraint
   *
   * @dataProvider embedCodeProvider
   */
  public function testTweetEmbedCodeConstraint($embed_code, $expected_violation_count) {

    // Check message in constraint.
    $constraint = new TweetEmbedCodeConstraint();
    $this
      ->assertEquals('Not valid Tweet URL/embed code.', $constraint->message, 'Correct constraint message found.');
    $execution_context = $this
      ->getMockBuilder('\\Drupal\\Core\\TypedData\\Validation\\ExecutionContext')
      ->disableOriginalConstructor()
      ->getMock();
    if ($expected_violation_count) {
      $execution_context
        ->expects($this
        ->exactly($expected_violation_count))
        ->method('addViolation')
        ->with($constraint->message);
    }
    else {
      $execution_context
        ->expects($this
        ->exactly($expected_violation_count))
        ->method('addViolation');
    }
    $validator = new TweetEmbedCodeConstraintValidator();
    $validator
      ->initialize($execution_context);
    $validator
      ->validate($this
      ->getMockFieldItem($embed_code), $constraint);
  }

  /**
   * Provides test data for testTweetEmbedCodeConstraint().
   */
  public function embedCodeProvider() {
    return [
      'valid tweet URL' => [
        'https://twitter.com/drupal8changes/status/649167396230578176',
        0,
      ],
      'valid tweet embed code' => [
        '<blockquote class="twitter-tweet" lang="en"><p lang="en" dir="ltr">EntityChangedInterface now also defines the function setChangedTime <a href="http://t.co/1Q58UcR8OY">http://t.co/1Q58UcR8OY</a></p>&mdash; Drupal 8 Changes (@drupal8changes) <a href="https://twitter.com/drupal8changes/status/649167396230578176">September 30, 2015</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>',
        0,
      ],
      'invalid URL' => [
        'https://drupal.org/project/media_entity_twitter',
        1,
      ],
      'invalid text' => [
        'I want my Tweet!',
        1,
      ],
      'invalid tweet URL' => [
        'https://twitter.com/drupal8changes/statustypo/649167396230578176',
        1,
      ],
      'invalid tweet ID' => [
        'https://twitter.com/drupal8changes/status/aa64916739bb6230578176',
        1,
      ],
    ];
  }

  /**
   * Tests TweetVisible constraint.
   *
   * @covers \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetVisibleConstraintValidator
   * @covers \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetVisibleConstraint
   *
   * @dataProvider visibleProvider
   */
  public function testTweetVisibleConstraint($embed_code, $mocked_response, $violations) {

    // Check message in constraint.
    $constraint = new TweetVisibleConstraint();
    $this
      ->assertEquals('Referenced tweet is not publicly visible.', $constraint->message, 'Correct constraint message found.');
    $http_client = $this
      ->getMock('\\GuzzleHttp\\Client');
    $http_client
      ->expects($this
      ->once())
      ->method('__call')
      ->with('get', [
      $embed_code,
      [
        'allow_redirects' => FALSE,
      ],
    ])
      ->willReturn($mocked_response);

    // Make sure no violations are raised for visible tweet.
    $execution_context = $this
      ->getMockBuilder('\\Drupal\\Core\\TypedData\\Validation\\ExecutionContext')
      ->disableOriginalConstructor()
      ->getMock();
    if ($violations) {
      $execution_context
        ->expects($this
        ->once())
        ->method('addViolation')
        ->with($constraint->message);
    }
    else {
      $execution_context
        ->expects($this
        ->exactly($violations))
        ->method('addViolation');
    }
    $validator = new TweetVisibleConstraintValidator($http_client);
    $validator
      ->initialize($execution_context);
    $validator
      ->validate($this
      ->getMockFieldItem($embed_code), $constraint);
  }

  /**
   * Provides test data for testTweetVisibleConstraint().
   */
  public function visibleProvider() {
    $visible_response = $this
      ->getMock('\\GuzzleHttp\\Psr7\\Response');
    $visible_response
      ->expects($this
      ->any())
      ->method('getStatusCode')
      ->will($this
      ->returnValue(200));
    $invisible_response = $this
      ->getMock('\\GuzzleHttp\\Psr7\\Response');
    $invisible_response
      ->expects($this
      ->once())
      ->method('getStatusCode')
      ->will($this
      ->returnValue(302));
    $invisible_response
      ->expects($this
      ->once())
      ->method('getHeader')
      ->with('location')
      ->will($this
      ->returnValue([
      'https://twitter.com/drupal8changes?protected_redirect=true',
    ]));
    return [
      'valid URL' => [
        'https://twitter.com/drupal8changes/status/649167396230578176',
        $visible_response,
        0,
      ],
      'invalid URL' => [
        'https://twitter.com/drupal8changes/status/649637310024273920',
        $invisible_response,
        1,
      ],
    ];
  }

  /**
   * Tests whether the TweetVisible constraint is robust against bad URLs.
   *
   * @covers \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetVisibleConstraintValidator
   * @covers \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetVisibleConstraint
   *
   * @dataProvider badUrlsProvider
   */
  public function testBadUrlsOnVisibleConstraint($embed_code) {
    $http_client = $this
      ->getMock('\\GuzzleHttp\\Client');
    $http_client
      ->expects($this
      ->never())
      ->method('get');
    $execution_context = $this
      ->getMockBuilder('\\Drupal\\Core\\TypedData\\Validation\\ExecutionContext')
      ->disableOriginalConstructor()
      ->getMock();
    $validator = new TweetVisibleConstraintValidator($http_client);
    $validator
      ->initialize($execution_context);
    $constraint = new TweetVisibleConstraint();
    $validator
      ->validate($this
      ->getMockFieldItem($embed_code), $constraint);
  }

  /**
   * Provides test data for testBadUrlsOnVisibleConstraint().
   */
  public function badUrlsProvider() {
    return [
      [
        'https://google.com',
      ],
      [
        'https://twitter.com/drupal/ssstatus/725771037837762561',
      ],
      [
        'https://twitter.com/drupal/status',
      ],
      [
        'https://twitter.com/drupal/status/foo',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConstraintsTest::badUrlsProvider public function Provides test data for testBadUrlsOnVisibleConstraint().
ConstraintsTest::embedCodeProvider public function Provides test data for testTweetEmbedCodeConstraint().
ConstraintsTest::getMockFieldItem protected function Creates a string_long FieldItemInterface wrapper around a value.
ConstraintsTest::testBadUrlsOnVisibleConstraint public function Tests whether the TweetVisible constraint is robust against bad URLs.
ConstraintsTest::testTweetEmbedCodeConstraint public function Tests TweetEmbedCode constraint.
ConstraintsTest::testTweetVisibleConstraint public function Tests TweetVisible constraint.
ConstraintsTest::visibleProvider public function Provides test data for testTweetVisibleConstraint().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340