NodeFormTest.php in URL Alias Permissions 8        
                          
                  
                        
  
  
  
  
  
File
  tests/src/Functional/NodeFormTest.php
  
    View source  
  <?php
namespace Drupal\Tests\url_alias_permissions\Functional;
use Drupal\Tests\BrowserTestBase;
class NodeFormTest extends BrowserTestBase {
  
  protected static $modules = [
    'url_alias_permissions_test',
    'node',
  ];
  
  protected $defaultTheme = 'stark';
  
  protected $userWithUrlAliasPermissions;
  
  protected $userWithoutUrlAliasPermissions;
  
  protected function setUp() : void {
    parent::setup();
    $this
      ->createContentType([
      'type' => 'page',
      'name' => 'Basic page',
      'display_submitted' => FALSE,
    ]);
    $this->userWithUrlAliasPermissions = $this
      ->drupalCreateUser([
      'create page content',
      'edit any page content',
      'create url aliases',
      'edit page node url alias',
    ]);
    $this->userWithoutUrlAliasPermissions = $this
      ->drupalCreateUser([
      'create page content',
      'edit any page content',
    ]);
  }
  
  public function testPathAliasFormElementAccess() {
    
    $this
      ->drupalLogin($this->userWithUrlAliasPermissions);
    $this
      ->drupalGet('/node/add/page');
    $this
      ->assertSession()
      ->fieldExists('path[0][alias]');
    
    $title = $this
      ->randomMachineName();
    $this
      ->submitForm([
      'title[0][value]' => $title,
      'path[0][alias]' => '/test-alias',
    ], 'Save');
    
    $node = $this
      ->drupalGetNodeByTitle($title);
    $this
      ->assertEquals('/test-alias', $node
      ->get('path')->alias);
    
    $this
      ->drupalGet('node/' . $node
      ->id() . '/edit');
    $this
      ->assertSession()
      ->fieldExists('path[0][alias]');
    
    $this
      ->drupalLogin($this->userWithoutUrlAliasPermissions);
    
    $this
      ->drupalGet('/node/add/page');
    $this
      ->assertSession()
      ->fieldNotExists('path[0][alias]');
    
    $this
      ->drupalGet('node/' . $node
      ->id() . '/edit');
    $this
      ->assertSession()
      ->fieldNotExists('path[0][alias]');
    
    $this
      ->submitForm([], 'Save');
    $node = $this
      ->drupalGetNodeByTitle($title, TRUE);
    $this
      ->assertEquals('/test-alias', $node
      ->get('path')->alias);
  }
}