You are here

public function JSDelivrTest::test_isAvailable in Libraries CDN API 7

@covers ::isAvailable()

File

tests/src/Plugin/LibrariesCDN/JSDelivrTest.php, line 99
Contains \Drupal\Tests\Libraries_cdn\Plugin\LibrariesCDN\JSDelivrTest.

Class

JSDelivrTest
@coversDefaultClass \Drupal\libraries_cdn\Plugin\LibrariesCDN\JSDelivr

Namespace

Drupal\Tests\libraries_cdn\Plugin\LibrariesCDN

Code

public function test_isAvailable() {

  // Test 1
  $this->plugin
    ->setLibrary('jquery');
  $configuration = $this->plugin
    ->getConfiguration();
  $url = sprintf($configuration['urls']['isAvailable'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => '{"results":[{"name":"test1"},{"name":"test2"}]}',
  ));

  // This is executed twice to test $configuration['available'].
  $this
    ->assertTrue($this->plugin
    ->isAvailable());
  $this
    ->assertTrue($this->plugin
    ->isAvailable());

  // Test 2
  $this->plugin
    ->setLibrary('Its a trap');
  $configuration = $this->plugin
    ->getConfiguration();
  $url = sprintf($configuration['urls']['isAvailable'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => '[]',
  ));

  // This is executed twice to test $configuration['available'].
  $this
    ->assertFalse($this->plugin
    ->isAvailable());
  $this
    ->assertFalse($this->plugin
    ->isAvailable());

  // Test 3
  $this->plugin
    ->setLibrary('jquery');
  $url = 'http://api.jsdelivr.com/fake_url?search=%s';
  $this->plugin
    ->setURL('isAvailable', $url);
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with(sprintf($url, $this->plugin
    ->getLibrary()), array())
    ->andReturn(array(
    'code' => 404,
  ));

  // This is executed twice to test $configuration['available'].
  $this
    ->assertFalse($this->plugin
    ->isAvailable());
  $this
    ->assertFalse($this->plugin
    ->isAvailable());

  // Test 4
  $this->plugin
    ->setLibrary('jquery');
  $configuration = $this->plugin
    ->getConfiguration();
  $url = sprintf($configuration['urls']['isAvailable'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => '{"results":[{"name":"test1"},{"name":"test2"}]}',
  ));

  // This is executed twice to test $configuration['available'].
  $this
    ->assertTrue($this->plugin
    ->isAvailable());
  $this
    ->assertTrue($this->plugin
    ->isAvailable());
}