You are here

public function CDNJSTest::test_getVersions in Libraries CDN API 7

@covers ::getVersions()

File

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

  // Test 1
  $this->plugin
    ->setLibrary('jquery');
  $results = array(
    'assets' => array(
      array(
        'name' => 'jquery',
        'files' => array(
          'test1.js',
          'test1.css',
        ),
        'version' => '1.0',
      ),
      array(
        'name' => 'jquery',
        'files' => array(
          'test2.js',
          'test2.css',
        ),
        'version' => '2.0',
      ),
      array(
        'name' => 'jquery',
        'files' => array(
          'test3.js',
          'test3.css',
        ),
        'version' => '3.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
  $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.cdnjs.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());
}