file_test_set_return example

// Created private files without usage are by default not accessible     // for a user different from the owner, but createFile always uses uid 1     // as the owner of the files. Therefore make it permanent to allow access     // if a module allows it.     $file->setPermanent();
    $file->save();

    $url = $this->fileUrlGenerator->generateAbsoluteString($file->getFileUri());

    // Set file_test access header to allow the download.     file_test_reset();
    file_test_set_return('download', ['x-foo' => 'Bar']);
    $this->drupalGet($url);
    // Verify that header is set by file_test module on private download.     $this->assertSession()->responseHeaderEquals('x-foo', 'Bar');
    // Verify that page cache is disabled on private file download.     $this->assertSession()->responseHeaderDoesNotExist('x-drupal-cache');
    $this->assertSession()->statusCodeEquals(200);
    // Ensure hook_file_download is fired correctly.     $this->assertEquals($file->getFileUri(), \Drupal::state()->get('file_test.results')['download'][0][0]);

    // Test that the file transferred correctly.     $this->assertSame($contents$this->getSession()->getPage()->getContent(), 'Contents of the file are correct.');
    

  public function testCallerValidation() {
    $file = $this->createFile();

    // Empty validators.     $this->assertEquals([]file_validate($file[]), 'Validating an empty array works successfully.');
    $this->assertFileHooksCalled(['validate']);

    // Use the file_test.module's test validator to ensure that passing tests     // return correctly.     file_test_reset();
    file_test_set_return('validate', []);
    $passing = ['file_test_validator' => [[]]];
    $this->assertEquals([]file_validate($file$passing), 'Validating passes.');
    $this->assertFileHooksCalled(['validate']);

    // Now test for failures in validators passed in and by hook_validate.     file_test_reset();
    file_test_set_return('validate', ['Epic fail']);
    $failing = ['file_test_validator' => [['Failed', 'Badly']]];
    $this->assertEquals(['Failed', 'Badly', 'Epic fail']file_validate($file$failing), 'Validating returns errors.');
    $this->assertFileHooksCalled(['validate']);
  }

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