public function UsageTest::testUsageTracking in Bynder 8
Same name and namespace in other branches
- 8.3 tests/src/Functional/UsageTest.php \Drupal\Tests\bynder\Functional\UsageTest::testUsageTracking()
- 8.2 tests/src/Functional/UsageTest.php \Drupal\Tests\bynder\Functional\UsageTest::testUsageTracking()
- 4.0.x tests/src/Functional/UsageTest.php \Drupal\Tests\bynder\Functional\UsageTest::testUsageTracking()
Tests Bynder usage tracking.
File
- tests/
src/ Functional/ UsageTest.php, line 101
Class
- UsageTest
- Tests the Bynder usage tracking.
Namespace
Drupal\Tests\bynder\FunctionalCode
public function testUsageTracking() {
$reference_node = $this
->createNode([
'type' => 'reference',
'title' => 'Reference node',
]);
$bynder_media = \Drupal::entityTypeManager()
->getStorage('media')
->create([
'name' => 'Media bynder',
'field_media_uuid' => '123',
'bundle' => 'media_type',
'type' => 'bynder',
]);
$bynder_media
->save();
$generic_media = \Drupal::entityTypeManager()
->getStorage('media')
->create([
'name' => 'Media generic',
'bundle' => 'generic',
'type' => 'generic',
]);
$generic_media
->save();
// Test with a node as referenced entity.
$this
->drupalGet('node/add/host');
$this
->getSession()
->getPage()
->fillField('title[0][value]', 'Host node');
$this
->getSession()
->getPage()
->fillField('field_reference_node[0][target_id]', $reference_node
->label() . ' (' . $reference_node
->id() . ')');
$this
->getSession()
->getPage()
->pressButton('Save');
$this
->assertEmpty(\Drupal::state()
->get('bynder.bynder_add_usage'), 'Add usage is not sent to Bynder for entities that are not of type Media.');
$host_node = current(\Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'title' => 'Host node',
]));
$this
->drupalGet('admin/config/search/path/add');
// Create alias.
$edit['source'] = '/node/' . $host_node
->id();
$edit['alias'] = '/' . $this
->randomMachineName(8);
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this
->drupalGet('node/' . $host_node
->id() . '/edit');
$this
->getSession()
->getPage()
->fillField('field_reference_node[0][target_id]', '');
$this
->getSession()
->getPage()
->pressButton('Save');
$this
->assertEmpty(\Drupal::state()
->get('bynder.bynder_delete_usage'), 'Remove usage is not sent to Bynder for entities that are not of type Media.');
// Test with media as referenced entity with generic type provider.
$this
->drupalGet('node/' . $host_node
->id() . '/edit');
$this
->getSession()
->getPage()
->fillField('field_reference_media[0][target_id]', $generic_media
->label() . ' (' . $generic_media
->id() . ')');
$this
->getSession()
->getPage()
->pressButton('Save');
$this
->assertEmpty(\Drupal::state()
->get('bynder.bynder_add_usage'), 'Add usage is not send to Bynder for media entities where the type provider is not Bynder.');
$this
->drupalGet('node/' . $host_node
->id() . '/edit');
$this
->getSession()
->getPage()
->fillField('field_reference_media[0][target_id]', '');
$this
->getSession()
->getPage()
->pressButton('Save');
$this
->assertEmpty(\Drupal::state()
->get('bynder.bynder_delete_usage'), 'Remove usage is not sent to Bynder for media entities where the type provider is not of type Bynder.');
// Test with media as referenced entity with bynder as type provider.
$this
->drupalGet('node/' . $host_node
->id() . '/edit');
$this
->getSession()
->getPage()
->fillField('field_reference_media[0][target_id]', $bynder_media
->label() . ' (' . $bynder_media
->id() . ')');
$this
->getSession()
->getPage()
->pressButton('Save');
$state = \Drupal::state()
->get('bynder.bynder_add_usage');
$this
->assertEquals(BynderApi::BYNDER_INTEGRATION_ID, $state['integration_id']);
$this
->assertEquals($bynder_media->field_media_uuid->value, $state['asset_id']);
$this
->assertTrue(is_string($state['timestamp']));
$url = \Drupal\Core\Url::fromRoute('entity.node.canonical', [
'node' => $host_node
->id(),
], [
'fragment' => 'node/' . $host_node
->id(),
])
->setAbsolute(TRUE)
->toString();
$this
->assertEquals($url, $state['location']
->setAbsolute(TRUE)
->toString());
$this
->assertEquals('Added asset by user ' . \Drupal::currentUser()
->getAccountName() . '.', $state['additional']);
$this
->drupalGet('node/' . $host_node
->id() . '/edit');
$this
->getSession()
->getPage()
->fillField('field_reference_media[0][target_id]', '');
$this
->getSession()
->getPage()
->pressButton('Save');
$state = \Drupal::state()
->get('bynder.bynder_delete_usage');
$this
->assertEquals(BynderApiTest::BYNDER_INTEGRATION_ID, $state['integration_id']);
$this
->assertEquals($bynder_media->field_media_uuid->value, $state['asset_id']);
}