1 path.test PathHooksTestCase::testPathHooks()

File

core/modules/path/tests/path.test, line 604
Tests for the Path module.

Class

PathHooksTestCase
Tests that path hooks are invoked.

Code

function testPathHooks() {
  // Create test node.
  $node1 = $this->backdropCreateNode();

  // Generate two test aliases.
  $alias1 = $this->randomName(8);
  $alias2 = $this->randomName(8);

  // Insert aliases and test that hook_path_insert() is called.
  $edit = array();
  $edit['source'] = 'node/' . $node1->nid;
  $edit['alias'] = $alias1;
  $this->backdropPost('admin/config/urls/path/add', $edit, t('Save URL alias'));
  $this->assertRaw('path_test_path_insert(): ' . $edit['alias'] . ' => ' . $edit['source'], t('hook_path_insert() was called.'));

  $edit['alias'] = $alias2;
  $this->backdropPost('admin/config/urls/path/add', $edit, t('Save URL alias'));

  // Extract the path ID from the second URL alias.
  $inserted_path = state_get('path_test_inserted_path', array());
  $pid2 = $inserted_path['pid'];

  // Update the second URL alias and test that hook_path_update() is called.
  $edit['alias'] = $alias2 = $this->randomName(8);
  $this->backdropPost('admin/config/urls/path/edit/' . $pid2, $edit, t('Save URL alias'));
  $this->assertRaw('path_test_path_update(): ' . $edit['alias'] . ' => ' . $edit['source'], t('hook_path_update() was called.'));

  // Delete the node and test that hook_path_delete() is called once for each
  // URL alias.
  $this->backdropPost('node/' . $node1->nid . '/delete', array(), t('Delete'));
  $this->assertRaw('path_test_path_delete(): ' . $alias1 . ' => node/' . $node1->nid, t('hook_path_delete() was called for the first URL alias.'));
  $this->assertRaw('path_test_path_delete(): ' . $alias2 . ' => node/' . $node1->nid, t('hook_path_delete() was called for the second URL alias.'));

  // Test that hook_path_delete() is not called if the node has no path
  // aliases.
  $node2 = $this->backdropCreateNode();
  $this->backdropPost('node/' . $node2->nid . '/delete', array(), t('Delete'));
  $this->assertNoRaw('path_test_path_delete()', t('hook_path_delete() was not called.'));
}