Because neither of these functions can reset their static internal caches, we can't test them in D6. I propose an optional $reset parameter to both of these functions, defaulted to FALSE so we don't mess up any APIs.

Comments

dave reid’s picture

Status: Active » Needs review
Issue tags: +pathauto
StatusFileSize
new1.6 KB

Patch attached for review.

This is the test that was failing because of the data leaking inbetween test runs:

class PathautoTaxonomyTokenTestCase extends PathautoTestHelper {
  protected $vocabularies = array();
  protected $terms = array();

  public static function getInfo() {
    return array(
      'name' => 'Pathauto taxonomy tokens',
      'description' => 'Unit tests for the taxonomy tokens provided by Pathauto.',
      'group' => 'Pathauto',
    );
  }

  function setUp() {
    parent::setUp('taxonomy');
    variable_set('pathauto_taxonomy_pattern', 'category/[vocab-raw]/[cat-raw]');
  }

  /**
   * Test the [termpath] token.
   */
  function testTermPath() {
  }

  /**
   * Test the [catpath] token.
   */
  function testCatPath() {
    $vocab = $this->addVocabulary();
    $term1 = $this->addTerm($vocab);
    $this->assertToken('taxonomy', $term1, 'catpath', $term1->name);

    $term2 = $this->addTerm($vocab, array('parent' => $term1->tid));
    $this->assertToken('taxonomy', $term2, 'catpath', "{$term1->name}/{$term2->name}");

    $term3 = $this->addTerm($vocab, array('parent' => $term2->tid));
    $this->assertToken('taxonomy', $term3, 'catpath', "{$term1->name}/{$term2->name}/{$term3->name}");
  }

  /**
   * Test the [catalias] token.
   */
  function testCatAlias() {
    $vocab = $this->addVocabulary();
    $term1 = $this->addTerm($vocab);
    // *** THIS IS WHERE THE TEST FAILED SINCE IT LOADED OLD DATA FOR BOTH VID 1 and TID 1.
    $this->assertToken('taxonomy', $term1, 'catalias', "category/{$vocab->name}/{$term1->name}");
  }

  function addVocabulary(array $vocabulary = array()) {
    $vocabulary += array(
      'name' => drupal_strtolower($this->randomName(5)),
      'nodes' => array('page' => 'page'),
    );
    taxonomy_save_vocabulary($vocabulary);
    return (object) $vocabulary;
  }

  function addTerm(stdClass $vocabulary, array $term = array()) {
    $term += array(
      'name' => drupal_strtolower($this->randomName(5)),
      'vid' => $vocabulary->vid,
    );
    taxonomy_save_term($term);
    return (object) $term;
  }
}
rfay’s picture

I guess I have two questions about this:

1. If it's leaking between tests, is it just that the tests are trying to tell us there's a flaw in the code? We shouldn't have to modify code being tested to get it to pass tests....

2. This is actually an API change, although it won't break anything, of course. Does it seem likely you can convince the powers that be that it's important enough?

Is there another way to solve this problem? Is there a bug in the code that we have to ferret out?

dave reid’s picture

1. Unfortunately is a bug in the code we'd only see when using tests. You could technically do the same thing in a standalone script that can use the Drupal APIs:

// Add a term
$new_term = array('name' => 'First term');
drupal_write_record('term_data', $term);

$loaded_term = taxonomy_get_term($new_term['tid']);
echo "Loaded term " . $loaded_term->name;
// Will print 'Loaded term First term'

// Delete the term
taxonomy_del_term($loaded_term->tid);

// Add another new term
$new_term = array('name' => 'Second term');
drupal_write_record('term_data', $term);

$loaded_term = taxonomy_get_term($new_term['tid']);
echo "Loaded term " . $loaded_term->name;
// Will print 'Loaded term First term' instead of 'Loaded term Second term'.

2. Well it's either we get this into D6 core, or we have to put anything like this into SimpleTests's core patch. It would be easier to fix it in core. But its not my decision to make.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

@Dave suppose you mean

// Add a term
$new_term = array('name' => 'First term');
drupal_write_record('term_data', $new_term);

This is really a core bug so let's wait Gabor's opinion

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, committed.

lfranck’s picture

Category: bug » feature
Status: Fixed » Needs work

As we fetch objects one by one, using tid or vid, why do we need to flush all the cache on reset = TRUE, instead of flushing the related tid|vid cache like, for example :

--  if ($reset) {
--   $terms = array();
-- }

+  if ($reset && array_key_exists($tid, $terms)) {
+    unset($terms[$tid]);
+  }
dave reid’s picture

Category: feature » bug
Status: Needs work » Fixed

The point is to clear the *entire* cache, not just for the individual term. This works exactly the same as other similar static caching functions in core:

function node_load($param = array(), $revision = NULL, $reset = NULL) {
  static $nodes = array();

  if ($reset) {
    $nodes = array();
  }

So I don't think it's appropriate for a follow-up.

Status: Fixed » Closed (fixed)
Issue tags: -pathauto

Automatically closed -- issue fixed for 2 weeks with no activity.