You are here

public function JSDelivrTest::test_getVersions in Libraries CDN API 7

@covers ::getVersions()

File

tests/src/Plugin/LibrariesCDN/JSDelivrTest.php, line 400
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_getVersions() {

  // Test 1
  $this->plugin
    ->setLibrary('jquery');
  $results = array(
    array(
      'name' => 'jquery',
      'files' => array(
        'test1.js',
        'test1.css',
      ),
      'version' => '1.0',
      'assets' => array(
        array(
          'version' => '1.10.0',
        ),
      ),
    ),
  );
  $urls = $this->plugin
    ->getConfiguration('urls');
  $url = sprintf($urls['isAvailable'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $url = sprintf($urls['getVersions'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $data = $this->plugin
    ->getVersions();
  $results = $this->plugin
    ->formatData('getVersions', $results);
  foreach ($data as $key => $result) {
    $this
      ->assertEquals($results[$key]['version'], $result);
  }

  // Test 2
  $results = array(
    array(
      'name' => 'jquery',
      'files' => array(
        'test1.js',
        'test1.css',
      ),
      'version' => '1.0',
      'assets' => array(
        array(
          'version' => '1.10.0',
        ),
      ),
    ),
  );
  $this->plugin
    ->setLibrary('jquery');
  $urls = $this->plugin
    ->getConfiguration('urls');
  $url = sprintf($urls['isAvailable'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $url = sprintf($urls['getVersions'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => '{"assets":"bonjour"}',
  ));
  $this
    ->assertEmpty($this->plugin
    ->getVersions());

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