ContributorTest.php in Bibliography & Citation 2.0.x        
                          
                  
                        
  
  
  
  
File
  modules/bibcite_entity/tests/src/Kernel/ContributorTest.php
  
    View source  
  <?php
namespace Drupal\Tests\bibcite_entity\Kernel;
use Drupal\bibcite_entity\Entity\Contributor;
use Drupal\KernelTests\KernelTestBase;
class ContributorTest extends KernelTestBase {
  public static $modules = [
    'system',
    'field',
    'bibcite',
    'bibcite_entity',
  ];
  
  
  protected $name = 'Mr. Jüan Martinez (Martin) de Lorenzo y Gutierez Jr.';
  
  protected $nameParts = [
    'prefix' => 'Mr.',
    'first_name' => 'Jüan',
    'middle_name' => 'Martinez',
    'last_name' => 'de Lorenzo y Gutierez',
    'nick' => 'Martin',
    'suffix' => 'Jr.',
  ];
  
  public function testContributorName() {
    $config = \Drupal::configFactory()
      ->getEditable('bibcite_entity.contributor.settings');
    $config
      ->set('full_name_pattern', '@prefix @first_name @middle_name @nick @last_name @suffix')
      ->save();
    $entity = Contributor::create($this->nameParts);
    $this
      ->assertEquals('Mr. Jüan Martinez Martin de Lorenzo y Gutierez Jr.', $entity->name->value);
    $config
      ->set('full_name_pattern', '@prefix @first_name @last_name @suffix')
      ->save();
    $this
      ->assertEquals('Mr. Jüan de Lorenzo y Gutierez Jr.', $entity->name->value);
    $entity = Contributor::create();
    $entity->name = $this->name;
    foreach ($this->nameParts as $part => $value) {
      $this
        ->assertEquals($value, $entity->{$part}->value);
    }
    $entity = Contributor::create();
    $entity->name = [
      $this->name,
    ];
    foreach ($this->nameParts as $part => $value) {
      $this
        ->assertEquals($value, $entity->{$part}->value);
    }
    $entity = Contributor::create();
    $entity->name = [
      'value' => $this->name,
    ];
    foreach ($this->nameParts as $part => $value) {
      $this
        ->assertEquals($value, $entity->{$part}->value);
    }
    $entity = Contributor::create();
    $entity->name = [
      [
        'value' => $this->name,
      ],
    ];
    foreach ($this->nameParts as $part => $value) {
      $this
        ->assertEquals($value, $entity->{$part}->value);
    }
  }
  
  public function testUnsetContributorName() {
    $entity = Contributor::create();
    $entity->name = $this->name;
    foreach ($this->nameParts as $part => $value) {
      $this
        ->assertEquals($value, $entity->{$part}->value);
    }
    $entity->name = NULL;
    foreach ($this->nameParts as $part => $value) {
      $this
        ->assertNull($entity->{$part}->value);
    }
  }
  
  public function testClearContributorName() {
    $entity = Contributor::create();
    $entity->name = $this->name;
    foreach ($this->nameParts as $part => $value) {
      $this
        ->assertEquals($value, $entity->{$part}->value);
    }
    $entity->name = '';
    foreach ($this->nameParts as $part => $value) {
      $this
        ->assertEquals('', $entity->{$part}->value);
    }
  }
}