You are here

public function ClassifiedTestTestBasicTest::test1491880 in Classified Ads 7.3

Test token generation for issue #1491880.

File

tests/classified_test_basic.test, line 458

Class

ClassifiedTestTestBasicTest
Basic test for known bugs in previous versions.

Code

public function test1491880() {
  $this->group = __FUNCTION__;
  $accounts = array(
    'creator',
  );
  $this
    ->createUsers($accounts);

  // 1. Get the Classified vocabulary id and field name.
  $vid = _classified_get('vid');
  $category_field_name = _classified_get('field-category');

  // 2a. Create a term in it, do not assign a specific lifetime.
  $term = (object) array(
    'name' => $this
      ->randomName(8),
    'description' => $this
      ->randomString(20),
    'vid' => $vid,
  );
  $status = taxonomy_term_save($term);
  $tid = $term->tid;
  $this
    ->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
    '@tid' => $tid,
  )), 'setup');

  // 2b. Create a published node bearing that term, belonging to "creator".
  // Avoid escaping problems and "/" to use a regexp.
  $title = check_plain($this
    ->randomName(20));
  $node = $this
    ->createNode(array(
    $category_field_name => array(
      LANGUAGE_NONE => array(
        0 => array(
          'tid' => $tid,
        ),
      ),
    ),
    'uid' => $this->creatorUser->uid,
    'title' => $title,
    'status' => 1,
  ));

  // Make sure tokens are visible.
  $tokens = token_info();
  $tokens = $tokens['tokens']['user'];
  $token_names = array(
    'classified-ads',
    'classified-ads-plain',
    'classified-ads-url',
  );
  foreach ($token_names as $token) {
    $this
      ->assertFalse(empty($tokens[$token]), t('%token token found in user tokens.', array(
      '%token' => $token,
    )), $this->group);
  }

  // Check token replacement.
  $token = 'classified-ads';
  $translation = token_replace("[user:{$token}]", array(
    'user' => $this->creatorUser,
  ));
  $this
    ->assertTrue(preg_match('/' . $node->title . '/', $translation), t('Node title from %token.', array(
    '%token' => $token,
  )), $this->group);
  $token = 'classified-ads-plain';
  $translation = token_replace("[user:{$token}]", array(
    'user' => $this->creatorUser,
  ));
  $this
    ->assertTrue(preg_match('/' . $node->title . '/', $translation), t('Node title from %token.', array(
    '%token' => $token,
  )), $this->group);
  $token = 'classified-ads-url';
  $translation = token_replace("[user:{$token}]", array(
    'user' => $this->creatorUser,
  ));
  $url = url('user/' . $node->uid . '/classified', array(
    'absolute' => TRUE,
  ));

  // URLs may contain a ?q=, which would be interpreted in a regex.
  $url = str_replace('?', '\\?', $url);
  $this
    ->assertTrue(preg_match("@{$url}@", $translation), t('User ads URL found from %token.', array(
    '%token' => $token,
  )), $this->group);
}