You are here

class LocalTest in Flysystem 8

Same name and namespace in other branches
  1. 3.x tests/src/Unit/Flysystem/LocalTest.php \Drupal\Tests\flysystem\Unit\Flysystem\LocalTest
  2. 2.0.x tests/src/Unit/Flysystem/LocalTest.php \Drupal\Tests\flysystem\Unit\Flysystem\LocalTest
  3. 3.0.x tests/src/Unit/Flysystem/LocalTest.php \Drupal\Tests\flysystem\Unit\Flysystem\LocalTest

@coversDefaultClass \Drupal\flysystem\Flysystem\Local @group flysystem

Hierarchy

Expanded class hierarchy of LocalTest

File

tests/src/Unit/Flysystem/LocalTest.php, line 19

Namespace

Drupal\Tests\flysystem\Unit\Flysystem
View source
class LocalTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $GLOBALS['base_url'] = 'http://example.com';
    $container = new ContainerBuilder();
    $url_generator = $this
      ->prophesize(UrlGeneratorInterface::class);
    $url_generator
      ->generateFromRoute(Argument::cetera())
      ->willReturn('download');
    $container
      ->set('url_generator', $url_generator
      ->reveal());
    \Drupal::setContainer($container);
    (new LocalAdapter('foo/bar'))
      ->deleteDir('');
    @rmdir('foo/bar');
    @rmdir('foo');
    mkdir('foo');
    mkdir('foo/bar');
    touch('test.txt');
  }

  /**
   * {@inheritdoc}
   */
  public function tearDown() {
    (new LocalAdapter('foo'))
      ->deleteDir('');
    @rmdir('foo');
    unlink('test.txt');
    @unlink('does_not_exist/.htaccess');
    @rmdir('does_not_exist');
  }

  /**
   * @covers ::__construct
   * @covers ::create
   */
  public function testCreateReturnsPlugin() {
    $container = new ContainerBuilder();
    $settings = new Settings([]);
    $container
      ->set('settings', $settings);
    $configuration = [
      'root' => 'foo/bar',
    ];
    $this
      ->assertInstanceOf(Local::class, Local::create($container, $configuration, '', []));
  }

  /**
   * @covers ::getAdapter
   * @covers ::ensureDirectory
   */
  public function testReturnsLocalAdapter() {
    $this
      ->assertInstanceOf(LocalAdapter::class, (new Local('foo/bar', FALSE))
      ->getAdapter());
  }

  /**
   * @covers ::getAdapter
   * @covers ::ensureDirectory
   */
  public function testMissingAdapterReturnedWhenPathIsFile() {
    $this
      ->assertInstanceOf(MissingAdapter::class, (new Local('test.txt'))
      ->getAdapter());
  }

  /**
   * @covers ::getExternalUrl
   */
  public function testReturnsValidLocalUrl() {
    $plugin = new Local('foo/bar', FALSE);
    $this
      ->assertSame('download', $plugin
      ->getExternalUrl('uri://test.html'));
  }

  /**
   * @covers ::getExternalUrl
   */
  public function testReturnsValidExternalUrl() {
    $plugin = new Local('foo/bar', TRUE);
    $this
      ->assertSame('http://example.com/foo/bar/test%20thing.html', $plugin
      ->getExternalUrl('uri://test thing.html'));
  }

  /**
   * @covers ::ensure
   * @covers ::ensureDirectory
   */
  public function testDirectoryIsAutoCreatedAndHtaccessIsWritten() {
    $plugin = new Local('does_not_exist');
    $this
      ->assertTrue(is_dir('does_not_exist'));
    $this
      ->assertTrue(is_file('does_not_exist/.htaccess'));
  }

  /**
   * @covers ::ensure
   * @covers ::writeHtaccess
   */
  public function testHtaccessNotOverwritten() {
    file_put_contents('foo/bar/.htaccess', 'htcontent');
    $result = (new Local('foo/bar'))
      ->ensure();
    $this
      ->assertSame(1, count($result));
    $this
      ->assertSame(RfcLogLevel::INFO, $result[0]['severity']);
    $this
      ->assertSame('htcontent', file_get_contents('foo/bar/.htaccess'));
  }

  /**
   * @covers ::ensure
   * @covers ::writeHtaccess
   */
  public function testHtaccessNotOverwrittenAndFails() {
    mkdir('foo/bar/.htaccess', 0777, TRUE);
    $result = (new Local('foo/bar'))
      ->ensure(TRUE);
    $this
      ->assertSame(1, count($result));
    $this
      ->assertSame('https://www.drupal.org/SA-CORE-2013-003', $result[0]['context']['@url']);
  }

  /**
   * @covers ::ensure
   * @covers ::writeHtaccess
   */
  public function testEnsureReturnsErrorWhenCantCreateDir() {
    $result = (new Local('test.txt'))
      ->ensure();
    $this
      ->assertSame('test.txt', $result[0]['context']['%root']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LocalTest::setUp public function Overrides UnitTestCase::setUp
LocalTest::tearDown public function
LocalTest::testCreateReturnsPlugin public function @covers ::__construct @covers ::create
LocalTest::testDirectoryIsAutoCreatedAndHtaccessIsWritten public function @covers ::ensure @covers ::ensureDirectory
LocalTest::testEnsureReturnsErrorWhenCantCreateDir public function @covers ::ensure @covers ::writeHtaccess
LocalTest::testHtaccessNotOverwritten public function @covers ::ensure @covers ::writeHtaccess
LocalTest::testHtaccessNotOverwrittenAndFails public function @covers ::ensure @covers ::writeHtaccess
LocalTest::testMissingAdapterReturnedWhenPathIsFile public function @covers ::getAdapter @covers ::ensureDirectory
LocalTest::testReturnsLocalAdapter public function @covers ::getAdapter @covers ::ensureDirectory
LocalTest::testReturnsValidExternalUrl public function @covers ::getExternalUrl
LocalTest::testReturnsValidLocalUrl public function @covers ::getExternalUrl
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.