0

I am attempting to write simpletests to replicate the steps to reproduce on this d.o issue.

I need to create a test node with a custom alias, as if I had unchecked the "Generate automatic URL alias" checkbox and supplied my own value.

Currently, attempting to insert it with

$edit = array(
  'title' => $this->randomName(),
  'path' => array(
    'source' => 'node/1',
    'alias' => 'qwerty',
  ),
);

fails, and generates errors, as does

 $edit = array(
    'title' => 'asdf',
    'path[alias]' => 'qwerty',
  );

So, I tried:

$path = array('source' => 'node/1', 'alias' => 'qwerty');
path_save($path);

The above successfully creates an alias, but when I inspect $node->path[ alias'] it shows me something other than qwerty.

drupal_lookup_path('alias', "node/".$node->nid) also returns only the Pathauto-generated alias, and not any alias that was created manually, even in the node edit form.

Additionally, if I create a new alias from the same source, my original alias is not replaced, as it would be if I had manually entered a new alias into the field on the node edit form.

As I am attempting to exactly duplicate the steps to reproduce in the linked d.o issue, it matters how the alias gets created. Is there any way to more exactly replicate the manual creation of an alias on the node edit form, inside a test?

1

It turns out that

$edit = array(
  'title' => 'asdf',
  'path[alias]' => 'qwerty',
);

was correct, but that I needed to add the permission 'create url aliases' to the drupalCreateUser array.

1

You can create custom alias using this code:

$path = array(
  'source' => 'node/' . $node->nid,
  'alias' => "content/' . $node->title,
);
path_save($path);

Use this code in your test just after you create the node.

And then you can retrieve the alias using this drupal_get_path_alias('node/' . $node->nid) in your test case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.