AuditFilesNotInDatabaseTest.php in Audit Files 8.2        
                          
                  
                        
  
  
  
  
File
  tests/src/Functional/AuditFilesNotInDatabaseTest.php
  
    View source  
  <?php
namespace Drupal\Tests\auditfiles\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\RoleInterface;
use Drupal\Core\Url;
use Drupal\Tests\TestFileCreationTrait;
class AuditFilesNotInDatabaseTest extends BrowserTestBase {
  use TestFileCreationTrait;
  
  protected static $modules = [
    'node',
    'field',
    'file',
    'user',
    'auditfiles',
  ];
  
  protected $user;
  
  protected $rid;
  
  protected $defaultTheme = 'stark';
  
  protected function setUp() : void {
    parent::setUp();
    
    $this->user = $this
      ->drupalCreateUser([
      'access audit files reports',
    ]);
    $all_rids = $this->user
      ->getRoles();
    unset($all_rids[array_search(RoleInterface::AUTHENTICATED_ID, $all_rids)]);
    
    $this->rid = reset($all_rids);
    
    $this
      ->getTestFiles('binary');
    $this
      ->getTestFiles('html');
    $this
      ->getTestFiles('image');
    $this
      ->getTestFiles('javascript');
    $this
      ->getTestFiles('php');
    $this
      ->getTestFiles('sql');
    $this
      ->getTestFiles('text');
  }
  
  public function testReportPage() {
    
    $path = URL::fromRoute('auditfiles.notindatabase');
    
    $session = $this
      ->assertSession();
    
    $this
      ->drupalGet($path);
    $session
      ->statusCodeEquals(403);
    
    $this
      ->drupalLogin($this->user);
    
    $this
      ->drupalGet($path);
    $session
      ->statusCodeEquals(200);
  }
  
  public function testFileNotInDatabase() {
    
    $path = URL::fromRoute('auditfiles.notindatabase');
    
    $session = $this
      ->assertSession();
    
    $this
      ->drupalLogin($this->user);
    
    $this
      ->drupalGet($path);
    
    $session
      ->pageTextContains("Not in database");
    
    $session
      ->elementNotContains('css', '#edit-files', 'No items found');
    $session
      ->pageTextNotContains("Found no files on the server that are not in the database");
    
    $session
      ->elementContains('xpath', '//*[@id="notindatabase"]/div[1]/em', "Found at least 36 files on the server that are not in the database");
  }
  
  public function testFileCanBeDeleted() {
    
    $path = URL::fromRoute('auditfiles.notindatabase');
    
    $session = $this
      ->assertSession();
    
    $this
      ->drupalLogin($this->user);
    
    $this
      ->drupalGet($path);
    
    $session
      ->pageTextContains("Not in database");
    $session
      ->elementExists('css', '#edit-files-html-2html');
    
    $edit = [
      'edit-files-html-2html' => TRUE,
    ];
    $this
      ->submitForm($edit, 'Delete selected items from the server');
    
    $session
      ->pageTextContains("Delete these files from the server?");
    $edit = [];
    $this
      ->submitForm($edit, 'Confirm');
    
    $session
      ->pageTextContains("Not in database");
    $session
      ->pageTextContains("Sucessfully deleted html-2.html from the server.");
    $session
      ->elementNotExists('css', '#edit-files-html-2html');
  }
  
  public function testFileCanBeAdded() {
    
    $path = URL::fromRoute('auditfiles.notindatabase');
    
    $session = $this
      ->assertSession();
    
    $this
      ->drupalLogin($this->user);
    
    $this
      ->drupalGet($path);
    
    $session
      ->pageTextContains("Not in database");
    $session
      ->elementExists('css', '#edit-files-image-1png');
    
    $edit = [
      'edit-files-image-1png' => TRUE,
    ];
    $this
      ->submitForm($edit, 'Add selected items to the database');
    
    $session
      ->pageTextContains("Add these files to the database?");
    $edit = [];
    $this
      ->submitForm($edit, 'Confirm');
    
    $session
      ->pageTextContains("Not in database");
    $session
      ->pageTextContains("Sucessfully added image-1.png to the database.");
    $session
      ->elementNotExists('css', '#edit-files-image-1png');
  }
}