You are here

public function CDNJSTest::test_isAvailable in Libraries CDN API 7

@covers ::isAvailable()

File

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

Class

CDNJSTest
@coversDefaultClass \Drupal\libraries_cdn\Plugin\LibrariesCDN\CDNJS

Namespace

Drupal\Tests\libraries_cdn\Plugin\LibrariesCDN

Code

public function test_isAvailable() {

  // Test 1
  $this->plugin
    ->setLibrary('jquery');
  $results = array(
    'results' => array(
      array(
        'name' => 'test1',
      ),
      array(
        'name' => 'test2',
      ),
      array(
        'name' => 'test3',
      ),
    ),
  );
  $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' => json_encode($results),
  ));

  // 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' => json_encode(array(
      'results' => array(),
    )),
  ));

  // 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.cdnjs.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());
}