getTestFiles example


  public function testFormSettingsSubmissionHandler($theme) {

    \Drupal::service('theme_installer')->install([$theme]);

    $page = $this->getSession()->getPage();
    $assert_session = $this->assertSession();

    $this->drupalGet("admin/appearance/settings/$theme");

    // Add a new managed file.     $file = current($this->getTestFiles('image'));
    $image_file_path = \Drupal::service('file_system')->realpath($file->uri);
    $page->attachFileToField('files[custom_logo]', $image_file_path);
    $assert_session->waitForButton('custom_logo_remove_button');

    // Assert the new file is uploaded as temporary. This file should not be     // saved as permanent if settings are not submitted.     $image_field = $this->assertSession()->hiddenFieldExists('custom_logo[fids]');
    $file = File::load($image_field->getValue());
    $this->assertFalse($file->isPermanent());

    $page->pressButton('Save configuration');
    

class ZipTest extends ArchiverTestBase {
  /** * {@inheritdoc} */
  protected $archiverPluginId = 'Zip';

  /** * Tests that the Zip archive is created if it does not exist. */
  public function testCreateArchive() {
    $textFile = current($this->getTestFiles('text'));
    $archiveFilename = $this->fileSystem->realpath('public://' . $this->randomMachineName() . '.zip');
    $zip = new Zip($archiveFilename[
      'flags' => \ZipArchive::CREATE,
    ]);
    $zip->add($this->fileSystem->realPath($textFile->uri));
    // Close the archive and make sure it is written to disk.     $this->assertTrue($zip->getArchive()->close(), 'Successfully closed archive.');
    $this->assertFileExists($archiveFilename, 'Archive is automatically created if the file does not exist.');
    $this->assertArchiveContainsFile($archiveFilename$this->fileSystem->realPath($textFile->uri));
  }

  
/** @var \Drupal\field\FieldConfigInterface $field */
    // Disable the alt text field, because this is not a JavaScript test and     // the alt text field will therefore not appear without a full page refresh.     $field = FieldConfig::load("media.$media_type_id.field_media_image");
    $settings = $field->getSettings();
    $settings['alt_field'] = TRUE;
    $settings['alt_field_required'] = FALSE;
    $field->set('settings', $settings);
    $field->save();

    $file = File::create([
      'uri' => $this->getTestFiles('image')[0]->uri,
    ]);
    $file->save();

    $media = Media::create([
      'name' => 'Custom name',
      'bundle' => $media_type_id,
      'field_media_image' => $file->id(),
    ]);
    $media->save();

    // Change only the alt of the image.
'status' => TRUE,
      'scheme' => 'public',
      'directory' => 'inline-images',
      'max_size' => '',
      'max_dimensions' => [
        'width' => 0,
        'height' => 0,
      ],
    ]);

    $url = $this->getUploadUrl();
    $image_file = file_get_contents(current($this->getTestFiles('image'))->uri);
    $non_image_file = file_get_contents(current($this->getTestFiles('php'))->uri);
    $response = $this->uploadRequest($url$non_image_file, 'test.php');
    $this->assertSame(422, $response->getStatusCode());

    $response = $this->uploadRequest($url$image_file, 'test.jpg');
    $this->assertSame(201, $response->getStatusCode());
  }

  /** * Tests using the file upload route with a file size larger than allowed. */
  
'default',
      ['target_bundles' => ['image']],
      FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
    );
    // Add the media field to the form display.     \Drupal::service('entity_display.repository')->getFormDisplay('node', 'article')
      ->setComponent('field_media', ['type' => 'media_library_widget'])
      ->save();

    // Create a file to user for our images.     $image = File::create([
      'uri' => $this->getTestFiles('image')[0]->uri,
    ]);
    $image->setPermanent();
    $image->save();

    // Create a translated and untranslated media item in each language.     // cSpell:disable     $media_items = [
      ['nl' => 'Eekhoorn', 'es' => 'Ardilla'],
      ['es' => 'Zorro', 'nl' => 'Vos'],
      ['nl' => 'Hert'],
      ['es' => 'Tejón'],
    ];
$web_driver = $this->getSession()->getDriver();

    $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
    $field_name = 'images';
    $storage_settings = ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED];
    $field_settings = ['alt_field_required' => 0];
    $this->createImageField($field_name, 'article', $storage_settings$field_settings);
    $this->drupalLogin($this->drupalCreateUser(['access content', 'create article content']));
    $this->drupalGet('node/add/article');
    $this->assertSession()->fieldExists('title[0][value]')->setValue('Test');

    $images = $this->getTestFiles('image');
    $images = array_slice($images, 0, 5);

    $paths = [];
    foreach ($images as $image) {
      $paths[] = $file_system->realpath($image->uri);
    }

    $remote_paths = [];
    foreach ($paths as $path) {
      $remote_paths[] = $web_driver->uploadFileAndGetRemoteFilePath($path);
    }

    
$this->assertSame([]array_map(
      function DConstraintViolation $v) {
        return (string) $v->getMessage();
      },
      iterator_to_array(CKEditor5::validatePair($this->editor, $filtered_html_format))
    ));

    // Create a sample media entity to be embedded.     $this->createMediaType('image', ['id' => 'image']);
    File::create([
      'uri' => $this->getTestFiles('image')[0]->uri,
    ])->save();
    $this->mediaImage = Media::create([
      'bundle' => 'image',
      'name' => 'Screaming hairy armadillo',
      'field_media_image' => [
        [
          'target_id' => 1,
          'alt' => 'default alt',
          'title' => 'default title',
        ],
      ],
    ]);
'administer filters',
      'use text format basic_html',
    ]);
    $this->drupalLogin($this->adminUser);
  }

  /** * Tests scaling of inline images. */
  public function testEditorUploadImageScale() {
    // Generate testing images.     $testing_image_list = $this->getTestFiles('image');

    // Case 1: no max dimensions set: uploaded image not scaled.     $test_image = $testing_image_list[0];
    [$image_file_width$image_file_height] = $this->getTestImageInfo($test_image->uri);
    $max_width = NULL;
    $max_height = NULL;
    $this->setMaxDimensions($max_width$max_height);
    $this->assertSavedMaxDimensions($max_width$max_height);
    [$uploaded_image_file_width$uploaded_image_file_height] = $this->uploadImage($test_image->uri);
    $this->assertEquals($image_file_width$uploaded_image_file_width);
    $this->assertEquals($image_file_height$uploaded_image_file_height);
    

  protected function doTestResponsiveImageFieldFormatters($scheme$empty_styles = FALSE) {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $field_name = mb_strtolower($this->randomMachineName());
    $this->createImageField($field_name, 'article', ['uri_scheme' => $scheme]);
    // Create a new node with an image attached. Make sure we use a large image     // so the scale effects of the image styles always have an effect.     $test_image = current($this->getTestFiles('image', 39325));

    // Create alt text for the image.     $alt = $this->randomMachineName();

    $nid = $this->uploadNodeImage($test_image$field_name, 'article', $alt);
    $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);

    // Test that the default formatter is being used.     $image_uri = File::load($node->{$field_name}->target_id)->getFileUri();
    $image = [
      


  /** * Tests importing configuration. */
  public function testImport() {
    // Verify access to the config upload form.     $this->drupalGet('admin/config/development/configuration/full/import');
    $this->assertSession()->statusCodeEquals(200);

    // Attempt to upload a non-tar file.     $text_file = $this->getTestFiles('text')[0];
    $edit = ['files[import_tarball]' => \Drupal::service('file_system')->realpath($text_file->uri)];
    $this->drupalGet('admin/config/development/configuration/full/import');
    $this->submitForm($edit, 'Upload');
    $this->assertSession()->pageTextContains('Could not extract the contents of the tar file');

    // Make the sync directory read-only.     $directory = Settings::get('config_sync_directory');
    \Drupal::service('file_system')->chmod($directory, 0555);
    $this->drupalGet('admin/config/development/configuration/full/import');
    $this->assertSession()->pageTextContains("The directory $directory is not writable.");
    // Ensure submit button for \Drupal\config\Form\ConfigImportForm is
    $form_display = \Drupal::service('entity_display.repository')->getFormDisplay('node', 'article', 'default');
    $form_display->setComponent('field_media', [
      'type' => 'media_library_widget',
    ])->save();

    // Configure the "Editorial" workflow to apply to image media.     $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()->addEntityTypeAndBundle('media', 'image');
    $workflow->save();

    $image = File::create([
      'uri' => $this->getTestFiles('image')[0]->uri,
    ]);
    $image->setPermanent();
    $image->save();

    // Create a draft, published and archived media item.     $draft_media = Media::create([
      'name' => 'Hoglet',
      'bundle' => 'image',
      'field_media_image' => $image,
      'moderation_state' => 'draft',
    ]);
    
$public_files_path = PublicStream::basePath();

    $http_base_url = preg_replace('/^https?/', 'http', $base_url);
    $https_base_url = preg_replace('/^https?/', 'https', $base_url);
    $files_path = base_path() . $public_files_path;
    $csrf_path = $public_files_path . '/' . implode('/', array_fill(0, substr_count($public_files_path, '/') + 1, '..'));

    $druplicon = 'core/misc/druplicon.png';
    $red_x_image = base_path() . 'core/misc/icons/e32700/error.svg';

    // Put a test image in the files directory.     $test_images = $this->getTestFiles('image');
    $test_image = $test_images[0]->filename;

    // Put a test image in the files directory with special filename.     // cspell:ignore tést fïle nàme     $special_filename = 'tést fïle nàme.png';
    $special_image = rawurlencode($special_filename);
    $special_uri = str_replace($test_images[0]->filename, $special_filename$test_images[0]->uri);
    \Drupal::service('file_system')->copy($test_images[0]->uri, $special_uri);

    // Create a list of test image sources.     // The keys become the value of the IMG 'src' attribute, the values are the

class ImageUploadAccessTest extends ImageUploadTest {

  /** * Test access to the CKEditor 5 image upload controller. */
  public function testCkeditor5ImageUploadRoute() {
    $this->createBasicFormat();
    $url = $this->getUploadUrl();
    $test_image = file_get_contents(current($this->getTestFiles('image'))->uri);

    // With no text editor, expect a 404.     $response = $this->uploadRequest($url$test_image, 'test.jpg');
    $this->assertSame(404, $response->getStatusCode());

    $editor = $this->createEditorWithUpload([
      'status' => FALSE,
      'scheme' => 'public',
      'directory' => 'inline-images',
      'max_size' => '',
      'max_dimensions' => [
        
public function testMultiValuedWidget() {
    $type_name = 'article';
    $field_name = 'test_file_field_1';
    $field_name2 = 'test_file_field_2';
    $cardinality = 3;
    $this->createFileField($field_name, 'node', $type_name['cardinality' => $cardinality]);
    $this->createFileField($field_name2, 'node', $type_name['cardinality' => $cardinality]);

    $page = $this->getSession()->getPage();
    $assert_session = $this->assertSession();

    $test_file = current($this->getTestFiles('text'));
    $test_file_path = \Drupal::service('file_system')
      ->realpath($test_file->uri);

    $this->drupalGet("node/add/$type_name");
    foreach ([$field_name2$field_name] as $each_field_name) {
      for ($delta = 0; $delta < 3; $delta++) {
        $page->attachFileToField('files[' . $each_field_name . '_' . $delta . '][]', $test_file_path);
        $this->assertNotNull($assert_session->waitForElementVisible('css', '[name="' . $each_field_name . '_' . $delta . '_remove_button"]'));
        $this->assertNull($assert_session->waitForButton($each_field_name . '_' . $delta . '_upload_button'));
      }
    }

    

class TarTest extends ArchiverTestBase {
  /** * {@inheritdoc} */
  protected $archiverPluginId = 'Tar';

  /** * Tests that the Tar archive is created if it does not exist. */
  public function testCreateArchive() {
    $textFile = current($this->getTestFiles('text'));
    $archiveFilename = $this->fileSystem->realpath('public://' . $this->randomMachineName() . '.tar');
    $tar = new Tar($archiveFilename);
    $tar->add($this->fileSystem->realPath($textFile->uri));
    $this->assertFileExists($archiveFilename, 'Archive is automatically created if the file does not exist.');
    $this->assertArchiveContainsFile($archiveFilename$this->fileSystem->realPath($textFile->uri));
  }

}
Home | Imprint | This part of the site doesn't use cookies.