PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/file/src/Tests/FileFieldTestBase.php

https://gitlab.com/geeta7/drupal
PHP | 305 lines | 150 code | 32 blank | 123 comment | 7 complexity | 914efee47481e8fb6b6ddda59404a066 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\file\Tests\FileFieldTestBase.
  5. */
  6. namespace Drupal\file\Tests;
  7. use Drupal\field\Entity\FieldStorageConfig;
  8. use Drupal\field\Entity\FieldConfig;
  9. use Drupal\file\FileInterface;
  10. use Drupal\simpletest\WebTestBase;
  11. use Drupal\file\Entity\File;
  12. /**
  13. * Provides methods specifically for testing File module's field handling.
  14. */
  15. abstract class FileFieldTestBase extends WebTestBase {
  16. /**
  17. * Modules to enable.
  18. *
  19. * @var array
  20. */
  21. public static $modules = array('node', 'file', 'file_module_test', 'field_ui');
  22. /**
  23. * An user with administration permissions.
  24. *
  25. * @var \Drupal\user\UserInterface
  26. */
  27. protected $adminUser;
  28. protected function setUp() {
  29. parent::setUp();
  30. $this->adminUser = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer node fields', 'administer node display', 'administer nodes', 'bypass node access'));
  31. $this->drupalLogin($this->adminUser);
  32. $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
  33. }
  34. /**
  35. * Retrieves a sample file of the specified type.
  36. */
  37. function getTestFile($type_name, $size = NULL) {
  38. // Get a file to upload.
  39. $file = current($this->drupalGetTestFiles($type_name, $size));
  40. // Add a filesize property to files as would be read by
  41. // \Drupal\file\Entity\File::load().
  42. $file->filesize = filesize($file->uri);
  43. return entity_create('file', (array) $file);
  44. }
  45. /**
  46. * Retrieves the fid of the last inserted file.
  47. */
  48. function getLastFileId() {
  49. return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
  50. }
  51. /**
  52. * Creates a new file field.
  53. *
  54. * @param string $name
  55. * The name of the new field (all lowercase), exclude the "field_" prefix.
  56. * @param string $entity_type
  57. * The entity type.
  58. * @param string $bundle
  59. * The bundle that this field will be added to.
  60. * @param array $storage_settings
  61. * A list of field storage settings that will be added to the defaults.
  62. * @param array $field_settings
  63. * A list of instance settings that will be added to the instance defaults.
  64. * @param array $widget_settings
  65. * A list of widget settings that will be added to the widget defaults.
  66. */
  67. function createFileField($name, $entity_type, $bundle, $storage_settings = array(), $field_settings = array(), $widget_settings = array()) {
  68. $field_storage = entity_create('field_storage_config', array(
  69. 'entity_type' => $entity_type,
  70. 'field_name' => $name,
  71. 'type' => 'file',
  72. 'settings' => $storage_settings,
  73. 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
  74. ));
  75. $field_storage->save();
  76. $this->attachFileField($name, $entity_type, $bundle, $field_settings, $widget_settings);
  77. return $field_storage;
  78. }
  79. /**
  80. * Attaches a file field to an entity.
  81. *
  82. * @param string $name
  83. * The name of the new field (all lowercase), exclude the "field_" prefix.
  84. * @param string $entity_type
  85. * The entity type this field will be added to.
  86. * @param string $bundle
  87. * The bundle this field will be added to.
  88. * @param array $field_settings
  89. * A list of field settings that will be added to the defaults.
  90. * @param array $widget_settings
  91. * A list of widget settings that will be added to the widget defaults.
  92. */
  93. function attachFileField($name, $entity_type, $bundle, $field_settings = array(), $widget_settings = array()) {
  94. $field = array(
  95. 'field_name' => $name,
  96. 'label' => $name,
  97. 'entity_type' => $entity_type,
  98. 'bundle' => $bundle,
  99. 'required' => !empty($field_settings['required']),
  100. 'settings' => $field_settings,
  101. );
  102. entity_create('field_config', $field)->save();
  103. entity_get_form_display($entity_type, $bundle, 'default')
  104. ->setComponent($name, array(
  105. 'type' => 'file_generic',
  106. 'settings' => $widget_settings,
  107. ))
  108. ->save();
  109. // Assign display settings.
  110. entity_get_display($entity_type, $bundle, 'default')
  111. ->setComponent($name, array(
  112. 'label' => 'hidden',
  113. 'type' => 'file_default',
  114. ))
  115. ->save();
  116. }
  117. /**
  118. * Updates an existing file field with new settings.
  119. */
  120. function updateFileField($name, $type_name, $field_settings = array(), $widget_settings = array()) {
  121. $field = FieldConfig::loadByName('node', $type_name, $name);
  122. $field->setSettings(array_merge($field->getSettings(), $field_settings));
  123. $field->save();
  124. entity_get_form_display('node', $type_name, 'default')
  125. ->setComponent($name, array(
  126. 'settings' => $widget_settings,
  127. ))
  128. ->save();
  129. }
  130. /**
  131. * Uploads a file to a node.
  132. *
  133. * @param \Drupal\file\FileInterface $file
  134. * The File to be uploaded.
  135. * @param string $field_name
  136. * The name of the field on which the files should be saved.
  137. * @param $nid_or_type
  138. * A numeric node id to upload files to an existing node, or a string
  139. * indicating the desired bundle for a new node.
  140. * @param bool $new_revision
  141. * The revision number.
  142. * @param array $extras
  143. * Additional values when a new node is created.
  144. *
  145. * @return int
  146. * The node id.
  147. */
  148. function uploadNodeFile(FileInterface $file, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = array()) {
  149. return $this->uploadNodeFiles([$file], $field_name, $nid_or_type, $new_revision, $extras);
  150. }
  151. /**
  152. * Uploads multiple files to a node.
  153. *
  154. * @param \Drupal\file\FileInterface[] $files
  155. * The files to be uploaded.
  156. * @param string $field_name
  157. * The name of the field on which the files should be saved.
  158. * @param $nid_or_type
  159. * A numeric node id to upload files to an existing node, or a string
  160. * indicating the desired bundle for a new node.
  161. * @param bool $new_revision
  162. * The revision number.
  163. * @param array $extras
  164. * Additional values when a new node is created.
  165. *
  166. * @return int
  167. * The node id.
  168. */
  169. function uploadNodeFiles(array $files, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = array()) {
  170. $edit = array(
  171. 'title[0][value]' => $this->randomMachineName(),
  172. 'revision' => (string) (int) $new_revision,
  173. );
  174. $node_storage = $this->container->get('entity.manager')->getStorage('node');
  175. if (is_numeric($nid_or_type)) {
  176. $nid = $nid_or_type;
  177. $node_storage->resetCache(array($nid));
  178. $node = $node_storage->load($nid);
  179. }
  180. else {
  181. // Add a new node.
  182. $extras['type'] = $nid_or_type;
  183. $node = $this->drupalCreateNode($extras);
  184. $nid = $node->id();
  185. // Save at least one revision to better simulate a real site.
  186. $node->setNewRevision();
  187. $node->save();
  188. $node_storage->resetCache(array($nid));
  189. $node = $node_storage->load($nid);
  190. $this->assertNotEqual($nid, $node->getRevisionId(), 'Node revision exists.');
  191. }
  192. // Attach files to the node.
  193. $field_storage = FieldStorageConfig::loadByName('node', $field_name);
  194. // File input name depends on number of files already uploaded.
  195. $field_num = count($node->{$field_name});
  196. $name = 'files[' . $field_name . "_$field_num]";
  197. if ($field_storage->getCardinality() != 1) {
  198. $name .= '[]';
  199. }
  200. foreach ($files as $file) {
  201. $file_path = $this->container->get('file_system')->realpath($file->getFileUri());
  202. if (count($files) == 1) {
  203. $edit[$name] = $file_path;
  204. }
  205. else {
  206. $edit[$name][] = $file_path;
  207. }
  208. }
  209. $this->drupalPostForm("node/$nid/edit", $edit, t('Save and keep published'));
  210. return $nid;
  211. }
  212. /**
  213. * Removes a file from a node.
  214. *
  215. * Note that if replacing a file, it must first be removed then added again.
  216. */
  217. function removeNodeFile($nid, $new_revision = TRUE) {
  218. $edit = array(
  219. 'revision' => (string) (int) $new_revision,
  220. );
  221. $this->drupalPostForm('node/' . $nid . '/edit', array(), t('Remove'));
  222. $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
  223. }
  224. /**
  225. * Replaces a file within a node.
  226. */
  227. function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
  228. $edit = array(
  229. 'files[' . $field_name . '_0]' => drupal_realpath($file->getFileUri()),
  230. 'revision' => (string) (int) $new_revision,
  231. );
  232. $this->drupalPostForm('node/' . $nid . '/edit', array(), t('Remove'));
  233. $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
  234. }
  235. /**
  236. * Asserts that a file exists physically on disk.
  237. */
  238. function assertFileExists($file, $message = NULL) {
  239. $message = isset($message) ? $message : format_string('File %file exists on the disk.', array('%file' => $file->getFileUri()));
  240. $this->assertTrue(is_file($file->getFileUri()), $message);
  241. }
  242. /**
  243. * Asserts that a file exists in the database.
  244. */
  245. function assertFileEntryExists($file, $message = NULL) {
  246. $this->container->get('entity.manager')->getStorage('file')->resetCache();
  247. $db_file = File::load($file->id());
  248. $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->getFileUri()));
  249. $this->assertEqual($db_file->getFileUri(), $file->getFileUri(), $message);
  250. }
  251. /**
  252. * Asserts that a file does not exist on disk.
  253. */
  254. function assertFileNotExists($file, $message = NULL) {
  255. $message = isset($message) ? $message : format_string('File %file exists on the disk.', array('%file' => $file->getFileUri()));
  256. $this->assertFalse(is_file($file->getFileUri()), $message);
  257. }
  258. /**
  259. * Asserts that a file does not exist in the database.
  260. */
  261. function assertFileEntryNotExists($file, $message) {
  262. $this->container->get('entity.manager')->getStorage('file')->resetCache();
  263. $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->getFileUri()));
  264. $this->assertFalse(File::load($file->id()), $message);
  265. }
  266. /**
  267. * Asserts that a file's status is set to permanent in the database.
  268. */
  269. function assertFileIsPermanent(FileInterface $file, $message = NULL) {
  270. $message = isset($message) ? $message : format_string('File %file is permanent.', array('%file' => $file->getFileUri()));
  271. $this->assertTrue($file->isPermanent(), $message);
  272. }
  273. }