createFileField example

parent::setUp();

    // Update the test node type to not create new revisions by default. This     // allows testing for cases when a new revision is made and when it isn't.     $node_type = NodeType::load('bundle_with_section_field');
    $node_type->setNewRevision(FALSE);
    $node_type->save();
    $field_settings = [
      'file_extensions' => 'txt',
      'uri_scheme' => 'private',
    ];
    $this->createFileField('field_file', 'block_content', 'basic', $field_settings);
    $this->fileSystem = $this->container->get('file_system');
  }

  /** * Tests access to private files added to inline blocks in the layout builder. */
  public function testPrivateFiles() {
    $assert_session = $this->assertSession();
    LayoutBuilderEntityViewDisplay::load('node.bundle_with_section_field.default')
      ->enableLayoutBuilder()
      ->setOverridable()
      
$node = Node::load($nid);
      $this->assertNotNull($node, 'The node was loaded successfully.');
    }
  }

  /** * Tests file submission for an anonymous visitor. */
  public function testAnonymousNodeWithFile() {
    $type = 'Article';
    $title = 'Test page';
    $this->createFileField('field_image', 'node', 'article', []['file_extensions' => 'txt png']);

    // Load the node form.     $this->drupalLogout();
    $this->drupalGet('node/add/article');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains("Create $type");

    // Generate an image file.     $image = $this->getTestFile('image');

    // Submit the form.


  /** * Tests file access for file uploaded to a private node. */
  public function testPrivateFile() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name['uri_scheme' => 'private']);

    $test_file = $this->getTestFile('text');
    $nid = $this->uploadNodeFile($test_file$field_name$type_name, TRUE, ['private' => TRUE]);
    \Drupal::entityTypeManager()->getStorage('node')->resetCache([$nid]);
    /** @var \Drupal\node\NodeInterface $node */
    $node = $node_storage->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);
    // Ensure the file can be viewed.     $this->drupalGet('node/' . $node->id());
    $this->assertSession()->responseContains($node_file->getFilename());
    // Ensure the file can be downloaded.
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests the validation message is displayed only once for ajax uploads. */
  public function testAjaxValidationMessage() {
    $field_name = strtolower($this->randomMachineName());
    $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
    $this->createFileField($field_name, 'node', 'article', []['file_extensions' => 'txt']);

    $this->drupalLogin($this->drupalCreateUser([
      'access content',
      'create article content',
    ]));

    $page = $this->getSession()->getPage();
    $this->drupalGet('node/add/article');
    $image_file = current($this->getTestFiles('image'));
    $image_path = $this->container->get('file_system')->realpath($image_file->uri);
    $page->attachFileToField('files[' . $field_name . '_0]', $image_path);
    

  protected function setUp(): void {
    parent::setUp();

    $this->fileSystem = $this->container->get('file_system');

    // Create the Article node type.     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);

    // Attach a file field to the node type.     $field_settings = ['file_extensions' => 'bin txt'];
    $this->createFileField('field_file', 'node', 'article', []$field_settings);

    // Log in as a content author who can create Articles.     $this->user = $this->drupalCreateUser([
      'access content',
      'create article content',
    ]);
    $this->drupalLogin($this->user);

    // Disable the displaying of errors, so that the AJAX responses are not     // contaminated with error messages about exceeding the maximum POST size.     // @todo Remove this when issue #2905597 is fixed.

  protected $defaultTheme = 'stark';

  /** * Tests the required property on file fields. */
  public function testRequired() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $storage = $this->createFileField($field_name, 'node', $type_name[]['required' => '1']);
    $field = FieldConfig::loadByName('node', $type_name$field_name);

    $test_file = $this->getTestFile('text');

    // Try to post a new node without uploading a file.     $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    $this->drupalGet('node/add/' . $type_name);
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains("{$field->getLabel()} field is required.");

    
    $this->config('file.settings')
      ->set('make_unused_managed_files_temporary', TRUE)
      ->save();

    // Create the "Basic page" node type.     // @todo Remove the disabling of new revision creation in     // https://www.drupal.org/node/1239558.     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page', 'new_revision' => FALSE]);

    // Create a file field on the "Basic page" node type.     $this->fieldName = strtolower($this->randomMachineName());
    $this->createFileField($this->fieldName, 'node', 'page');

    // Create and log in user.     $permissions = [
      'access administration pages',
      'administer content translation',
      'administer content types',
      'administer languages',
      'create content translations',
      'create page content',
      'edit any page content',
      'translate any entity',
      
$field_name = strtolower($this->randomMachineName());
    $type_name = 'article';
    $field_storage_settings = [
      'display_field' => '1',
      'display_default' => '1',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ];
    $field_settings = [
      'description_field' => '1',
    ];
    $widget_settings = [];
    $this->createFileField($field_name, 'node', $type_name$field_storage_settings$field_settings$widget_settings);

    // Create a new node *without* the file field set, and check that the field     // is not shown for each node display.     $node = $this->drupalCreateNode(['type' => $type_name]);
    // Check file_default last as the assertions below assume that this is the     // case.     $file_formatters = ['file_table', 'file_url_plain', 'hidden', 'file_default'];
    foreach ($file_formatters as $formatter) {
      if ($formatter === 'hidden') {
        $edit = [
          "fields[$field_name][region]" => 'hidden',
        ];
// This test expects unused managed files to be marked as a temporary file.     $this->config('file.settings')
      ->set('make_unused_managed_files_temporary', TRUE)
      ->save();

    $this->adminUser = $this->drupalCreateUser([
      'access files overview',
      'bypass node access',
      'delete any file',
    ]);
    $this->baseUser = $this->drupalCreateUser();
    $this->createFileField('file', 'node', 'article', []['file_extensions' => 'txt png']);
  }

  /** * Calculates total count of usages for a file. * * @param array $usage * Array of file usage information as returned from file_usage subsystem. * * @return int * Total usage count. */
  

  public function testFileTokenReplacement() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $token_service = \Drupal::token();
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = $this->container->get('date.formatter');

    // Create file field.     $type_name = 'article';
    $field_name = 'field_' . strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name);

    $test_file = $this->getTestFile('text');
    // Coping a file to test uploads with non-latin filenames.     // cSpell:disable-next-line     $filename = \Drupal::service('file_system')->dirname($test_file->getFileUri()) . '/текстовый файл.txt';
    $test_file = \Drupal::service('file.repository')->copy($test_file$filename);

    // Create a new node with the uploaded file.     $nid = $this->uploadNodeFile($test_file$field_name$type_name);

    // Load the node and the file.

  protected $defaultTheme = 'stark';

  /** * Tests RSS enclosure formatter display for RSS feeds. */
  public function testFileFieldRSSContent() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $field_name = strtolower($this->randomMachineName());
    $type_name = 'article';

    $this->createFileField($field_name, 'node', $type_name);

    // RSS display must be added manually.     $this->drupalGet("admin/structure/types/manage/$type_name/display");
    $edit = [
      "display_modes_custom[rss]" => '1',
    ];
    $this->submitForm($edit, 'Save');

    // Change the format to 'RSS enclosure'.     $this->drupalGet("admin/structure/types/manage/$type_name/display/rss");
    $edit = [
      "
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    // Create the "Basic page" node type.     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);

    // Create a file field on the "Basic page" node type.     $this->fieldName = strtolower($this->randomMachineName());
    $this->createFileField($this->fieldName, 'node', 'page', ['uri_scheme' => 'private']);

    // Create and log in user.     $permissions = [
      'access administration pages',
      'administer content translation',
      'administer content types',
      'administer languages',
      'create content translations',
      'create page content',
      'edit any page content',
      'translate any entity',
    ];
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $storage_settings = ['cardinality' => 3];
    $this->createFileField('field_file', 'entity_test', 'entity_test', $storage_settings);
    $this->drupalLogin($this->drupalCreateUser([
      'administer entity_test content',
      'access content',
    ]));
  }

  /** * Tests uploading remote files. */
  public function testGetRemoteFilePath() {
    $web_driver = $this->getSession()->getDriver();
    
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests the custom access handler is invoked. */
  public function testFileAccessHandler() {
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name);
    \Drupal::state()->set('file_test_alternate_access_handler', TRUE);
    \Drupal::entityTypeManager()->clearCachedDefinitions();
    $test_file = $this->getTestFile('text');
    $nid = $this->uploadNodeFile($test_file$field_name$type_name);
    $this->drupalGet('node/' . $nid);
    $this->assertTrue(\Drupal::state()->get('file_access_formatter_check', FALSE));
  }

}

  public function testRevisions() {
    // This test expects unused managed files to be marked as a temporary file     // and then deleted up by file_cron().     $this->config('file.settings')
      ->set('make_unused_managed_files_temporary', TRUE)
      ->save();
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $this->createFileField($field_name, 'node', $type_name);
    // Create the same fields for users.     $this->createFileField($field_name, 'user', 'user');

    $test_file = $this->getTestFile('text');

    // Create a new node with the uploaded file.     $nid = $this->uploadNodeFile($test_file$field_name$type_name);

    // Check that the file exists on disk and in the database.     $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);
    
Home | Imprint | This part of the site doesn't use cookies.