samedi 28 avril 2018

Drupal 8 Functional Testing Node Update Fails

I'm trying to write a function test for some behaviour that will occur in hook_node_presave()

In the test I create two nodes and test the association between them, which works fine.

class TestNewsItemsAreReassociatedTest extends BrowserTestBase {

  /**
   * The installation profile to use with this test.
   *
   * @var string
   */
  protected $profile = 'standard';

  /**
   * Enabled modules
   */
  public static $modules = [
    'custom_services_industries',
  ];

  /**
   * {@inheritdoc}
   */
  function setUp() {
    parent::setUp();
  }

  /**
   * Test associations change.
   */
  public function testAssociationsChange() {

    // Create nodes to test.
    $service = $this->drupalCreateNode(['type' => 'service_and_industry']);
    $news = $this->drupalCreateNode(['type' => 'news', 'field_related_services' => $service->id()]);

    // Check the service is related to the news item.
    $relatedService = $news->get('field_related_services')->getValue();
    $this->assertEquals([0 => ['target_id' => $service->id()]], $relatedService);

    // Check the news is not related to an industry.
    $relatedIndustry = $news->get('field_related_industries')->getValue();
    $this->assertEmpty($relatedIndustry);

  }

}

Then I need to update a field value in one node, which I attempt to to do like this

// Change the service type.
$service = node_load($service->id());
$service->set('field_service_type', ['industry']);
$service->save();

But this fails with the error

Drupal\Core\Entity\EntityStorageException : SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘test_site.node__field_related_services' doesn't exist:

All the tables in the database are prefixed with test73350856 but surely Drupal would take care of database prefixes.

How do I update a field’s value and then save the node in a functional test?

Aucun commentaire:

Enregistrer un commentaire