FieldExampleBrowserTestBase.php
      
       
 
  
  
  Namespace
  Drupal\Tests\field_example\Functional
File
  - 
              modules/field_example/tests/src/Functional/FieldExampleBrowserTestBase.php
    
   
  
    View source
  
  <?php
namespace Drupal\Tests\field_example\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\examples\Functional\ExamplesBrowserTestBase;
abstract class FieldExampleBrowserTestBase extends ExamplesBrowserTestBase {
  
  protected $defaultTheme = 'stark';
  
  protected $contentTypeName;
  
  protected $administratorAccount;
  
  protected $authorAccount;
  
  protected $fieldName;
  
  protected static $modules = [
    'block',
    'node',
    'field_ui',
    'field_example',
  ];
  
  protected function setUp() : void {
    
    parent::setUp();
    
    $permissions = [
      'administer content types',
      'administer node fields',
      'administer node form display',
      'administer node display',
    ];
    $this->administratorAccount = $this->drupalCreateUser($permissions);
    parent::drupalLogin($this->administratorAccount);
    
    $this->contentTypeName = strtolower($this->randomMachineName(10));
    $this->drupalGet('admin/structure/types/add');
    $edit = [
      'name' => $this->contentTypeName,
      'type' => $this->contentTypeName,
    ];
    $this->submitForm($edit, 'Save and manage fields');
    $this->assertSession()
      ->pageTextContains((string) new FormattableMarkup('The content type @name has been added.', [
      '@name' => $this->contentTypeName,
    ]));
    
    $create_permission = 'create ' . $this->contentTypeName . ' content';
    $this->checkPermissions([
      $create_permission,
    ]);
    
    
    $this->authorAccount = $this->drupalCreateUser([
      $create_permission,
    ]);
  }
  
  protected function createField($type = 'field_example_rgb', $widget_type = 'field_example_text', $cardinality = '1', $fieldFormatter = 'field_example_simple_text') {
    $assert = $this->assertSession();
    $this->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/fields');
    
    $this->clickLink('Create a new field');
    
    $field_name = strtolower($this->randomMachineName(10));
    
    $edit = [
      'new_storage_type' => $type,
      'field_name' => $field_name,
      'label' => $field_name,
    ];
    $this->submitForm($edit, 'Save and continue');
    
    
    $edit = [
      'cardinality' => 'number',
      'cardinality_number' => (string) $cardinality,
    ];
    
    
    if (-1 == $cardinality) {
      $edit = [
        'cardinality' => '-1',
        'cardinality_number' => '1',
      ];
    }
    
    $this->submitForm($edit, 'Save field settings');
    $assert->pageTextContains((string) new FormattableMarkup('Updated field @name field settings.', [
      '@name' => $field_name,
    ]));
    
    $this->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/form-display');
    $edit = [
      'fields[field_' . $field_name . '][type]' => $widget_type,
    ];
    $this->submitForm($edit, 'Save');
    
    $this->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/display');
    $edit1 = [
      'fields[field_' . $field_name . '][type]' => $fieldFormatter,
    ];
    $this->submitForm($edit1, 'Save');
    return $field_name;
  }
}
 
Classes