drupalGetTestFiles example

$this->submitForm($edit, 'Preview');
    $this->assertSession()->assertEscaped('<em>' . $this->webUser->id() . '</em>');

    \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
    $this->drupalGet('node/' . $this->node->id());
    $this->submitForm($edit, 'Preview');
    $this->assertInstanceOf(MarkupInterface::class$this->webUser->getDisplayName());
    $this->assertSession()->assertNoEscaped('<em>' . $this->webUser->id() . '</em>');
    $this->assertSession()->responseContains('<em>' . $this->webUser->id() . '</em>');

    // Add a user picture.     $image = current($this->drupalGetTestFiles('image'));
    $user_edit['files[user_picture_0]'] = \Drupal::service('file_system')->realpath($image->uri);
    $this->drupalGet('user/' . $this->webUser->id() . '/edit');
    $this->submitForm($user_edit, 'Save');

    // As the web user, fill in the comment form and preview the comment.     $this->drupalGet('node/' . $this->node->id());
    $this->submitForm($edit, 'Preview');

    // Check that the preview is displaying the title and body.     $this->assertSession()->titleEquals('Preview comment | Drupal');
    $this->assertSession()->pageTextContains($edit['subject[0][value]']);
    
'type' => 'text_textarea_with_summary',
      ])
      ->setComponent($file_field_name[
        'type' => 'file_generic',
      ])
      ->save();
    $display_repository->getViewDisplay('entity_test', 'entity_test', 'full')
      ->setComponent($text_field_name)
      ->setComponent($file_field_name)
      ->save();

    $test_file = current($this->drupalGetTestFiles('text'));
    $edit['files[file_field_0]'] = \Drupal::service('file_system')->realpath($test_file->uri);
    $this->drupalGet('entity_test/add');
    $this->submitForm($edit, 'Upload');
    $this->assertSession()->statusCodeEquals(200);
    $edit = [
      'text_long[0][value]' => 'Long text',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->statusCodeEquals(200);
    $this->drupalGet('entity_test/1');
    $this->assertSession()->pageTextContains('Long text');
  }
public function testPrivateLanguageFile() {
    // Verify that the file field on the "Basic page" node type is translatable.     $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page');
    $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node file field is translatable.');

    // Create a default language node.     $default_language_node = $this->drupalCreateNode(['type' => 'page']);

    // Edit the node to upload a file.     $edit = [];
    $name = 'files[' . $this->fieldName . '_0]';
    $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[0]->uri);
    $this->drupalGet('node/' . $default_language_node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $last_fid_prior = $this->getLastFileId();

    // Languages are cached on many levels, and we need to clear those caches.     $this->rebuildContainer();

    // Ensure the file can be downloaded.     \Drupal::entityTypeManager()->getStorage('node')->resetCache([$default_language_node->id()]);
    $node = Node::load($default_language_node->id());
    $node_file = File::load($node->{$this->fieldName}->target_id);
    
// Go to manage display page again.     $this->drupalGet("admin/structure/types/manage/article/display");

    // Test for absence of link to image styles configuration.     $this->submitForm([], "{$field_name}_settings_edit");
    $this->assertSession()->linkByHrefNotExists(Url::fromRoute('entity.image_style.collection')->toString(), 'Link to image styles configuration is absent when permissions are insufficient');

    // Restore 'administer image styles' permission to testing admin user     user_role_change_permissions(reset($admin_user_roles)['administer image styles' => TRUE]);

    // Create a new node with an image attached.     $test_image = current($this->drupalGetTestFiles('image'));

    // Ensure that preview works.     $this->previewNodeImage($test_image$field_name, 'article');

    // After previewing, make the alt field required. It cannot be required     // during preview because the form validation will fail.     $instance->setSetting('alt_field_required', 1);
    $instance->save();

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

    
    $this->drupalGet('admin/appearance/settings/olivero');
    $this->assertSession()->statusCodeEquals(404);
    // Ensure a non existent theme settings form URL returns 404.     $this->drupalGet('admin/appearance/settings/' . $this->randomMachineName());
    $this->assertSession()->statusCodeEquals(404);
    // Ensure a hidden theme settings form URL returns 404.     $this->assertTrue(\Drupal::service('theme_installer')->install(['stable9']));
    $this->drupalGet('admin/appearance/settings/stable9');
    $this->assertSession()->statusCodeEquals(404);

    // Specify a filesystem path to be used for the logo.     $file = current($this->drupalGetTestFiles('image'));
    $file_relative = strtr($file->uri, ['public:/' => PublicStream::basePath()]);
    $default_theme_path = 'core/themes/starterkit_theme';

    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    $file_url_generator = \Drupal::service('file_url_generator');
    $supported_paths = [
      // Raw stream wrapper URI.       $file->uri => [
        'form' => StreamWrapperManager::getTarget($file->uri),
        'src' => $file_url_generator->generateString($file->uri),
      ],
      
/** * Tests if managed file form element works well with multiple files upload. */
  public function testMultipleFilesUpload() {
    $file_system = \Drupal::service('file_system');
    $file_storage = \Drupal::entityTypeManager()->getStorage('file');
    $page = $this->getSession()->getPage();

    $this->drupalGet(Url::fromRoute('file_module_test.managed_test', ['multiple' => TRUE]));

    $paths = [];
    foreach (array_slice($this->drupalGetTestFiles('image'), 0, 2) as $image) {
      $paths[] = $image->filename;
      $page->attachFileToField('files[nested_file][]', $file_system->realpath($image->uri));
      $this->assertSession()->assertWaitOnAjaxRequest();
    }

    // Save entire form.     $page->pressButton('Save');

    $this->assertSession()->pageTextContains('The file ids are 1,2.');
    $this->assertCount(2, $file_storage->loadByProperties(['filename' => $paths]));
  }

}
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
  }

  /** * Retrieves a sample file of the specified type. * * @return \Drupal\file\FileInterface * The new unsaved file entity. */
  public function getTestFile($type_name$size = NULL) {
    // Get a file to upload.     $file = current($this->drupalGetTestFiles($type_name$size));

    // Add a filesize property to files as would be read by     // \Drupal\file\Entity\File::load().     $file->filesize = filesize($file->uri);

    return File::create((array) $file);
  }

  /** * Retrieves the fid of the last inserted file. */
  
$this->submitForm($edit, 'Save');

    // Create a translation in French.     $this->clickLink('Translate');
    $this->clickLink('Add');
    $this->submitForm([], 'Save (this translation)');
    $this->clickLink('Translate');

    // Edit English translation.     $this->clickLink('Edit', 1);
    // Upload and image after changing the node language.     $images = $this->drupalGetTestFiles('image')[1];
    $edit = [
      'langcode[0][value]' => 'de',
      'files[field_image_field_0]' => $images->uri,
    ];
    $this->submitForm($edit, 'Upload');
    $this->submitForm(['field_image_field[0][alt]' => 'alternative_text'], 'Save (this translation)');

    // Check that the translation languages are correct.     $node = $this->getNodeByTitle('english_title');
    $translation_languages = $node->getTranslationLanguages();
    $this->assertArrayHasKey('fr', $translation_languages);
    
/** * Tests upload, extraction, and update of a module. */
  public function testUploadModule() {
    // Ensure that the update information is correct before testing.     update_get_available(TRUE);

    // Images are not valid archives, so get one and try to install it. We     // need an extra variable to store the result of drupalGetTestFiles()     // since reset() takes an argument by reference and passing in a constant     // emits a notice in strict mode.     $imageTestFiles = $this->drupalGetTestFiles('image');
    $invalidArchiveFile = reset($imageTestFiles);
    $edit = [
      'files[project_upload]' => $invalidArchiveFile->uri,
    ];
    // This also checks that the correct archive extensions are allowed.     $this->drupalGet('admin/modules/install');
    $this->submitForm($edit, 'Continue');
    $extensions = \Drupal::service('plugin.manager.archiver')->getExtensions();
    $this->assertSession()->pageTextContains("Only files with the following extensions are allowed: $extensions.");
    $this->assertSession()->addressEquals('admin/modules/install');

    
protected $defaultTheme = 'stark';

  /** * Tests the validation message is displayed only once for ajax uploads. */
  public function testAJAXValidationMessage() {
    $field_name = strtolower($this->randomMachineName());
    $this->createImageField($field_name, 'article', ['cardinality' => -1]);

    $this->drupalGet('node/add/article');
    /** @var \Drupal\file\FileInterface[] $text_files */
    $text_files = $this->drupalGetTestFiles('text');
    $text_file = reset($text_files);

    $field = $this->getSession()->getPage()->findField('files[' . $field_name . '_0][]');
    $field->attachFile($this->container->get('file_system')->realpath($text_file->uri));
    $this->assertSession()->waitForElement('css', '.messages--error');

    // Verify that Ajax validation messages are displayed only once.     $this->assertSession()->elementsCount('xpath', '//div[contains(@class, "messages--error")]', 1);
  }

  /** * Tests that image field validation works with other form submit handlers. */
// Verify that the image field on the "Basic basic" node type is     // translatable.     $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'basicpage');
    $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node image field is translatable.');

    // Create a default language node.     $default_language_node = $this->drupalCreateNode(['type' => 'basicpage', 'title' => 'Lost in translation']);

    // Edit the node to upload a file.     $edit = [];
    $name = 'files[' . $this->fieldName . '_0]';
    $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[0]->uri);
    $this->drupalGet('node/' . $default_language_node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $edit = [$this->fieldName . '[0][alt]' => 'Lost in translation image', $this->fieldName . '[0][title]' => 'Lost in translation image title'];
    $this->submitForm($edit, 'Save');
    $first_fid = $this->getLastFileId();

    // Translate the node into French: remove the existing file.     $this->drupalGet('node/' . $default_language_node->id() . '/translations/add/en/fr');
    $this->submitForm([], 'Remove');

    // Upload a different file.
return \Drupal::state()->get('image_test.results', []);
  }

  /** * Sets up an image with the custom toolkit. * * @return \Drupal\Core\Image\ImageInterface * The image object. */
  protected function getImage(): ImageInterface {
    $image_factory = \Drupal::service('image.factory');
    $file = current($this->drupalGetTestFiles('image'));
    $image = $image_factory->get($file->uri, 'test');
    $this->assertTrue($image->isValid());
    return $image;
  }

  /** * Asserts the effect processing of an image effect plugin. * * @param string[] $expected_operations * String array containing the operation names, e.g. load, save, crop, etc. * @param string $effect_name * The name of the image effect to test. * @param array $data * The data to be passed to the image effect. */

  protected $image;

  /** * Tests \Drupal\Core\Form\FormState::cleanValues(). */
  public function testFormStateValuesCleanAdvanced() {

    // Get an image for uploading.     $image_files = $this->drupalGetTestFiles('image');
    $this->image = current($image_files);

    // Check if the physical file is there.     $this->assertFileExists($this->image->uri);

    // "Browse" for the desired file.     $edit = ['files[image]' => \Drupal::service('file_system')->realpath($this->image->uri)];

    // Post the form.     $this->drupalGet('form_test/form-state-values-clean-advanced');
    $this->submitForm($edit, 'Submit');

    

  protected $imageExtension;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $account = $this->drupalCreateUser(['access site reports']);
    $this->drupalLogin($account);

    $image_files = $this->drupalGetTestFiles('image');
    $this->image = File::create((array) current($image_files));

    [$this->imageExtension] = explode('.', $this->image->getFilename());
    $this->assertFileExists($this->image->getFileUri());

    $this->phpfile = current($this->drupalGetTestFiles('php'));
    $this->assertFileExists($this->phpfile->uri);

    $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      

  protected $defaultTheme = 'stark';

  protected $profile = 'testing';

  /** * Tests styled image dimensions cumulatively. */
  public function testImageDimensions() {
    $image_factory = $this->container->get('image.factory');
    // Create a working copy of the file.     $files = $this->drupalGetTestFiles('image');
    $file = reset($files);
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $original_uri = $file_system->copy($file->uri, 'public://', FileSystemInterface::EXISTS_RENAME);

    // Create a style.     /** @var \Drupal\image\ImageStyleInterface $style */
    $style = ImageStyle::create(['name' => 'test', 'label' => 'Test']);
    $style->save();
    $generated_uri = 'public://styles/test/public/' . $file_system->basename($original_uri);
    /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
    
Home | Imprint | This part of the site doesn't use cookies.