You are here

protected function AuditFilesUsedNotReferencedTest::setUp in Audit Files 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/AuditFilesUsedNotReferencedTest.php \Drupal\Tests\auditfiles\Functional\AuditFilesUsedNotReferencedTest::setUp()

Overrides BrowserTestBase::setUp

File

tests/src/Functional/AuditFilesUsedNotReferencedTest.php, line 60

Class

AuditFilesUsedNotReferencedTest
Tests that the "Managed not used" report is reachable with no errors.

Namespace

Drupal\Tests\auditfiles\Functional

Code

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

  // Create user with permissions to access audit files reports.
  $this->user = $this
    ->drupalCreateUser([
    'access audit files reports',
  ]);
  $all_rids = $this->user
    ->getRoles();
  unset($all_rids[array_search(RoleInterface::AUTHENTICATED_ID, $all_rids)]);

  // Save role IDs.
  $this->rid = reset($all_rids);

  // Creating the content type.
  // Create node based content type with image field.
  $bundle = 'article';
  $fieldName = 'field_image';

  // Create the content type.
  $content_type = NodeType::create([
    'type' => $bundle,
    'name' => 'Test Article',
  ]);
  $content_type
    ->save();

  // Replaces call to $this->createFileField from FileFieldCreationTrait.
  // Can't use FileFieldCreationTrait method because it has "type" hardcoded
  // as "file", and we need type "image".
  $settings = [
    'cardinality' => 1,
    'file_directory' => 'test_images',
    'file_extensions' => 'png gif jpg jpeg txt',
  ];
  $field_storage = FieldStorageConfig::create([
    'entity_type' => 'node',
    'field_name' => $fieldName,
    'type' => 'image',
    'settings' => $settings,
    'cardinality' => $settings['cardinality'],
  ]);
  $field_storage
    ->save();
  $this
    ->attachFileField($fieldName, 'node', $bundle, $settings, []);

  // End of $this->createFileField substitute call
  // End of Create the content type.
  // Next setup step...
  // Create files & nodes.
  $files = $this
    ->getTestFiles('image');
  $counter = 0;
  foreach ($files as $file) {
    $file->filesize = filesize($file->uri);
    $file->status = TRUE;
    $file->filename = $file->uri;
    $newfile = File::create((array) $file);
    $newfile
      ->save();
    $counter++;
    $node = Node::create([
      'type' => 'article',
      'title' => 'Sample Node ' . $counter,
      'field_image' => [
        'target_id' => $counter,
        'alt' => 'Sample ' . $counter,
      ],
    ]);
    $node
      ->save();
  }
}