You are here

public function ConstraintsTest::testTweetVisibleConstraint in Media entity Twitter 8

Tests TweetVisible constraint.

@covers \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetVisibleConstraintValidator @covers \Drupal\media_entity_twitter\Plugin\Validation\Constraint\TweetVisibleConstraint

@dataProvider visibleProvider

File

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

Class

ConstraintsTest
Tests media_entity_twitter constraints.

Namespace

Drupal\Tests\media_entity_twitter\Unit

Code

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);
}