You are here

FieldClearingNodeTest.php in Acquia Content Hub 8

File

tests/src/Functional/FieldClearingNodeTest.php
View source
<?php

namespace Drupal\Tests\acquia_contenthub\Functional;

use Drupal\Component\Serialization\Json;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Test that Acquia Content Hub respects Field Сlearing.
 *
 * @group acquia_contenthub
 */
class FieldClearingNodeTest extends ContentHubTestBase {
  use StringTranslationTrait;

  /**
   * The permissions of the admin user.
   *
   * @var string[]
   */
  protected $adminUserPermissions = [
    'bypass node access',
    'administer acquia content hub',
    'administer content types',
    'access administration pages',
    'administer node fields',
  ];

  /**
   * Admin role.
   *
   * @var string
   */
  protected $adminRole;

  /**
   * Modules to enable for this test.
   *
   * @var string[]
   */
  public static $modules = [
    'node',
    'acquia_contenthub',
    'field',
    'field_ui',
    'user',
  ];

  /**
   * Configure content hub node form.
   */
  public function testFieldClearing() {
    $this->adminRole = $this
      ->createAdminRole('administrator', 'Administrator');
    $this
      ->drupalLogin($this->adminUser);

    // Create Article Node.
    $entity = $this
      ->drupalCreateNode([
      'type' => 'article',
      'title' => 'Test Title',
      'body' => [
        [
          'value' => 'Test Body',
          'format' => filter_default_format(),
        ],
      ],
    ]);
    $this
      ->configureContentHubContentTypes('node', [
      'article',
    ]);

    // Rendering the CDF, we can see the "Body" Field.
    $output = $this
      ->drupalGetCdf('acquia-contenthub-cdf/' . $entity
      ->getEntityTypeId() . '/' . $entity
      ->id(), [
      'query' => [
        'include_references' => 'true',
      ],
    ]);
    $this
      ->assertSession()
      ->statusCodeEquals(200);

    // Check the base fields in CDF.
    $this
      ->assertEquals($output['entities']['0']['uuid'], $entity
      ->uuid());
    $this
      ->assertEquals($output['entities']['0']['attributes']['title']['value']['en'], 'Test Title');

    // Check the body in CDF.
    $json_body = $output['entities']['0']['attributes']['body']['value']['en'][0];
    $body = Json::decode($json_body);
    $this
      ->assertEquals($body['value'], 'Test Body');

    // Now modify article "Body" Field to have clear it.
    $node_edit_url = 'node/' . $entity
      ->id() . '/edit';
    $edit = [];
    $edit['body[0][value]'] = '';
    $this
      ->drupalPostForm($node_edit_url, $edit, $this
      ->t('Save'));

    // Rendering the CDF, we can see the "Body" Field.
    $output = $this
      ->drupalGetCdf('acquia-contenthub-cdf/' . $entity
      ->getEntityTypeId() . '/' . $entity
      ->id(), [
      'query' => [
        'include_references' => 'true',
      ],
    ]);
    $this
      ->assertSession()
      ->statusCodeEquals(200);

    // Check the base fields in CDF.
    $this
      ->assertEquals($output['entities']['0']['uuid'], $entity
      ->uuid());
    $this
      ->assertEquals($output['entities']['0']['attributes']['title']['value']['en'], 'Test Title');

    // Check the body in CDF.
    $json_body = $output['entities']['0']['attributes']['body']['value']['en'];
    $body = Json::decode($json_body);
    $this
      ->assertEquals($body, NULL);
  }

}

Classes

Namesort descending Description
FieldClearingNodeTest Test that Acquia Content Hub respects Field Сlearing.