2015-08-17 17:00:26 -07:00
< ? php
/**
* @ file
* Contains \Drupal\file\Tests\LoadTest .
*/
namespace Drupal\file\Tests ;
2015-08-27 12:03:05 -07:00
use Drupal\file\Entity\File ;
2015-08-17 17:00:26 -07:00
/**
2015-08-27 12:03:05 -07:00
* Tests \Drupal\file\Entity\File :: load () .
2015-08-17 17:00:26 -07:00
*
* @ group file
*/
class LoadTest extends FileManagedUnitTestBase {
/**
* Try to load a non - existent file by fid .
*/
function testLoadMissingFid () {
2015-08-27 12:03:05 -07:00
$this -> assertFalse ( File :: load ( - 1 ), 'Try to load an invalid fid fails.' );
2015-08-17 17:00:26 -07:00
$this -> assertFileHooksCalled ( array ());
}
/**
* Try to load a non - existent file by URI .
*/
function testLoadMissingFilepath () {
$files = entity_load_multiple_by_properties ( 'file' , array ( 'uri' => 'foobar://misc/druplicon.png' ));
$this -> assertFalse ( reset ( $files ), " Try to load a file that doesn't exist in the database fails. " );
$this -> assertFileHooksCalled ( array ());
}
/**
* Try to load a non - existent file by status .
*/
function testLoadInvalidStatus () {
$files = entity_load_multiple_by_properties ( 'file' , array ( 'status' => - 99 ));
$this -> assertFalse ( reset ( $files ), 'Trying to load a file with an invalid status fails.' );
$this -> assertFileHooksCalled ( array ());
}
/**
* Load a single file and ensure that the correct values are returned .
*/
function testSingleValues () {
// Create a new file entity from scratch so we know the values.
$file = $this -> createFile ( 'druplicon.txt' , NULL , 'public' );
2015-08-27 12:03:05 -07:00
$by_fid_file = File :: load ( $file -> id ());
2015-08-17 17:00:26 -07:00
$this -> assertFileHookCalled ( 'load' );
2015-08-27 12:03:05 -07:00
$this -> assertTrue ( is_object ( $by_fid_file ), '\Drupal\file\Entity\File::load() returned an object.' );
2015-08-17 17:00:26 -07:00
$this -> assertEqual ( $by_fid_file -> id (), $file -> id (), 'Loading by fid got the same fid.' , 'File' );
$this -> assertEqual ( $by_fid_file -> getFileUri (), $file -> getFileUri (), 'Loading by fid got the correct filepath.' , 'File' );
$this -> assertEqual ( $by_fid_file -> getFilename (), $file -> getFilename (), 'Loading by fid got the correct filename.' , 'File' );
$this -> assertEqual ( $by_fid_file -> getMimeType (), $file -> getMimeType (), 'Loading by fid got the correct MIME type.' , 'File' );
$this -> assertEqual ( $by_fid_file -> isPermanent (), $file -> isPermanent (), 'Loading by fid got the correct status.' , 'File' );
$this -> assertTrue ( $by_fid_file -> file_test [ 'loaded' ], 'file_test_file_load() was able to modify the file during load.' );
}
/**
* This will test loading file data from the database .
*/
function testMultiple () {
// Create a new file entity.
$file = $this -> createFile ( 'druplicon.txt' , NULL , 'public' );
// Load by path.
file_test_reset ();
$by_path_files = entity_load_multiple_by_properties ( 'file' , array ( 'uri' => $file -> getFileUri ()));
$this -> assertFileHookCalled ( 'load' );
2015-08-27 12:03:05 -07:00
$this -> assertEqual ( 1 , count ( $by_path_files ), 'entity_load_multiple_by_properties() returned an array of the correct size.' );
2015-08-17 17:00:26 -07:00
$by_path_file = reset ( $by_path_files );
$this -> assertTrue ( $by_path_file -> file_test [ 'loaded' ], 'file_test_file_load() was able to modify the file during load.' );
$this -> assertEqual ( $by_path_file -> id (), $file -> id (), 'Loading by filepath got the correct fid.' , 'File' );
// Load by fid.
file_test_reset ();
2015-08-27 12:03:05 -07:00
$by_fid_files = File :: loadMultiple ( array ( $file -> id ()));
2015-08-17 17:00:26 -07:00
$this -> assertFileHooksCalled ( array ());
2015-08-27 12:03:05 -07:00
$this -> assertEqual ( 1 , count ( $by_fid_files ), '\Drupal\file\Entity\File::loadMultiple() returned an array of the correct size.' );
2015-08-17 17:00:26 -07:00
$by_fid_file = reset ( $by_fid_files );
$this -> assertTrue ( $by_fid_file -> file_test [ 'loaded' ], 'file_test_file_load() was able to modify the file during load.' );
$this -> assertEqual ( $by_fid_file -> getFileUri (), $file -> getFileUri (), 'Loading by fid got the correct filepath.' , 'File' );
}
/**
* Loads a single file and ensure that the correct values are returned .
*/
public function testUuidValues () {
// Create a new file entity from scratch so we know the values.
$file = $this -> createFile ( 'druplicon.txt' , NULL , 'public' );
$file -> save ();
file_test_reset ();
$by_uuid_file = \Drupal :: entityManager () -> loadEntityByUuid ( 'file' , $file -> uuid ());
$this -> assertFileHookCalled ( 'load' );
$this -> assertTrue ( is_object ( $by_uuid_file ), '\Drupal::entityManager()->loadEntityByUuid() returned a file object.' );
if ( is_object ( $by_uuid_file )) {
$this -> assertEqual ( $by_uuid_file -> id (), $file -> id (), 'Loading by UUID got the same fid.' , 'File' );
$this -> assertTrue ( $by_uuid_file -> file_test [ 'loaded' ], 'file_test_file_load() was able to modify the file during load.' );
}
}
}