You are here

public function CDNJSTest::test_getFiles in Libraries CDN API 7

@covers ::getFiles()

File

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

  // 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['getFiles'], $this->plugin
    ->getLibrary());
  $this->drupal7
    ->shouldReceive('drupal_http_request')
    ->once()
    ->with($url, array())
    ->andReturn(array(
    'code' => 200,
    'data' => json_encode($results),
  ));
  $data = $this->plugin
    ->getFiles();
  $results = $this->plugin
    ->formatData('getFiles', $results);
  $this
    ->assertCount(count($results), $data);

  // Test 2
  $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
    ->getFiles());
}