? 127484-pathfilter_files_D6.patch
? pathfilter-i18n-node-alias.patch
Index: pathfilter.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/pathfilter/pathfilter.module,v
retrieving revision 1.5.2.1.2.4
diff -u -p -r1.5.2.1.2.4 pathfilter.module
--- pathfilter.module	7 May 2009 21:29:42 -0000	1.5.2.1.2.4
+++ pathfilter.module	8 May 2009 00:33:49 -0000
@@ -63,7 +63,11 @@ function pathfilter_filter($op, $delta =
         // The actual filtering is performed here. The supplied text should be
         // returned, once any necessary substitutions have taken place.
         case 'process':
-          return preg_replace_callback('/(["\'])internal:([^"#\?\']+)\??([^"#\']+)?#?([^"\']+)?(\1)/', "_pathfilter_process", $text);
+          $patterns = array();
+          $replacement = array();
+          $patterns[] = '/(["\'])(internal):([^"#\?\']+)\??([^"#\']+)?#?([^"\']+)?\1/';
+          $patterns[] = '/(["\'])(files):([\w\/.-]+)\1/';
+          return preg_replace_callback($patterns, "_pathfilter_process", $text);
 
         // Filter settings for pathfilter.
         case 'settings':
@@ -74,20 +78,34 @@ function pathfilter_filter($op, $delta =
 }
 
 function _pathfilter_process($matches) {
-  drupal_debug($matches);
-  $absolute = (variable_get('pathfilter_link_type', 'absolute') == 'absolute' ? 'TRUE' : 'FALSE');
+  //drupal_debug($matches);
+  switch ($matches[2]) {
+    case 'internal':
+      return _pathfilter_process_internal($matches);
+      break;
+      
+    case 'files':
+      return _pathfilter_process_files($matches);
+  }
+}
+
+function _pathfilter_process_internal($matches) {
+ $absolute = (variable_get('pathfilter_link_type', 'absolute') == 'absolute' ? TRUE : FALSE);
   if (module_exists('i18n')) {
-    if (preg_match('/(node\/([0-9]+))$/', $matches[2], $match)) {
+    if (preg_match('/(node\/([0-9]+))$/', $matches[3], $match)) {
       // if we have a drupal node url, ensure we get the translated url alias
       $languages = language_list('enabled');
       $languages = $languages[1];
       $language = $languages[i18n_node_get_lang($match[2])];
-      return stripcslashes($matches[1]). url($match[1], array('language' => $language, 'query' => $matches[3], 'fragment' => $matches[4], 'absolute' => $absolute )).stripcslashes($matches[1]);
+      return stripcslashes($matches[2]) . url($match[1], array('language' => $language, 'query' => $matches[4], 'fragment' => $matches[5], 'absolute' => $absolute )) . stripcslashes($matches[1]);
     }
   }
-  return stripcslashes($matches[1]). url($matches[2], array('query' => $matches[3], 'fragment' => $matches[4], 'absolute' => $absolute )).stripcslashes($matches[1]);
+  return stripcslashes($matches[1]) . url($matches[3], array('query' => $matches[4], 'fragment' => $matches[5], 'absolute' => $absolute )) . stripcslashes($matches[1]);
 }
 
+function _pathfilter_process_files($matches) {
+  return stripcslashes($matches[1]) . file_create_url($matches[3], array('absolute' => $absolute )) . stripcslashes($matches[1]);
+}
 
 /**
  * Helper settings function for hook_filter('settings').
Index: tests/pathfilter.test
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/pathfilter/tests/Attic/pathfilter.test,v
retrieving revision 1.1.2.3
diff -u -p -r1.1.2.3 pathfilter.test
--- tests/pathfilter.test	7 May 2009 21:00:49 -0000	1.1.2.3
+++ tests/pathfilter.test	8 May 2009 00:33:49 -0000
@@ -19,30 +19,180 @@ class PathfilterTestCase extends DrupalW
     );
   }
 
+  /**
+   * Implementation of setUp().
+   */
   function setUp() {
+    parent::setUp();
+  }
+  
+  /**
+   * Get a sample file of the specified type.
+   */
+  function getTestFile($type, $size = NULL) {
+    // Get a file to upload.
+    $file = current($this->drupalGetTestFiles($type, $size));
+
+    // SimpleTest files incorrectly use "filename" instead of "filepath".
+    $file->filepath = $file->filename;
+    $file->filename = basename($file->filepath);
+    $file->filesize = filesize($file->filepath);
+
+    return $file;
+  }
+  
+  /**
+   * Upload a file to a node.
+   */
+  function uploadNodeFile($file, $field_name, $nid_or_type, $new_revision = TRUE) {
+    $edit = array(
+      'title' => $this->randomName(),
+      'revision' => (string) (int) $new_revision,
+    );
+
+    if (is_numeric($nid_or_type)) {
+      $node = node_load($nid_or_type);
+      $delta = isset($node->$field_name) ? count($node->$field_name) : 0;
+      $edit['files[' . $field_name . '_' . $delta . ']'] = realpath($file->filepath);
+      $this->drupalPost('node/' . $nid_or_type . '/edit', $edit, t('Save'));
+    }
+    else {
+      $edit['files[' . $field_name . '_0]'] = realpath($file->filepath);
+      $type = str_replace('_', '-', $nid_or_type);
+      $this->drupalPost('node/add/' . $type, $edit, t('Save'));
+    }
+
+    $matches = array();
+    preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
+    return $matches[1];
+  }
+
+  function CompareSnippets($snippets) {
+    foreach ($snippets as $before => $after) {
+      $result = pathfilter_filter('process', 0, -1, $before);    
+      if ($after) {
+        $this->assertTrue($result == $after, 'Parsing: '.$before .'<br/> Expected: '. $after. '<br/> But got: '. $result);
+      }
+      else {
+        $this->assertTrue($result == $before, 'Pathfilter should not parse: '. $before.'<br/> But got: '.$result);
+      }
+    }
   }
   
   /**
   * test_pathfilter
+  **/
+  function testInternalRelative() {
+    variable_set('pathfilter_link_type', 'relative');
+    
+    // Create node for testing.
+    $node = $this->drupalCreateNode();
+
+    $snippets = array(      
+      '"internal:admin/user"'                 => '"/admin/user"',
+      '\'internal:admin/user\''               => "'/admin/user'", // single quotes
+      '"internal:node/nid"'                   => '"/node/nid"',
+      '"internal:node/'. $node->nid .'"'        => '"'. drupal_get_path_alias('/node/'. $node->nid) .'"',
+      '"internal:node/nid?page=1#section2"'   => '"/node/nid?page=1#section2"',
+    );
+    $this->CompareSnippets($snippets);
+  }
+  
+/**
+  * test_pathfilter
   * 
   * TODO: Write a description of the tests!
   **/
-  function test_pathfilter() {
+  function testInternalAbsolute() {
     global $base_url;
+    variable_set('pathfilter_link_type', 'absolute');
     
     // Create node for testing.
     $node = $this->drupalCreateNode();
-    //drupal_debug($node);
     
     $snippets = array(
-      '"test::link"'                          => PATHFILTER_NOT_OK,
-      '"internal:admin/user"'                 => '"'. $base_url.'/admin/user"',
-      '\'internal:admin/user\''               => '\''. $base_url.'/admin/user\'', // single quotes
-      '"internal:admin/user\''                => PATHFILTER_NOT_OK, // mismatch quotes
-      '\'internal:admin/user"'                => PATHFILTER_NOT_OK, // mismatch quotes
-      '"internal:node/nid"'                   => '"'. $base_url.'/node/nid"',
-      '"internal:node/'.$node->nid.'"'        => '"'. $base_url.'/'.drupal_get_path_alias('node/'.$node->nid).'"',
-      '"internal:node/nid?page=1#section2"'    => '"'. $base_url.'/node/nid?page=1#section2"',
+      '"internal:admin/user"'                 => '"'. $base_url .'/admin/user"',
+      '\'internal:admin/user\''               => '\''. $base_url .'/admin/user\'', // single quotes
+      '"internal:node/nid"'                   => '"'. $base_url .'/node/nid"',
+      '"internal:node/'. $node->nid .'"'        => '"'. $base_url .'/'. drupal_get_path_alias('node/'. $node->nid) .'"',
+      '"internal:node/nid?page=1#section2"'   => '"'. $base_url .'/node/nid?page=1#section2"',
+    );
+    $this->CompareSnippets($snippets);
+  }
+    
+  /**
+  * test_pathfilter
+  * 
+  * TODO: Write a description of the tests!
+  **/
+  function testFilesRelative() {
+    variable_set('pathfilter_link_type', 'relative');
+    
+    $snippets = array(
+      "'files:images/somefile.jpg'" => "'". file_create_url('images/somefile.jpg', array('absolute' => $absolute )) ."'", // single quotes
+      '"files:images/somefile.jpg"' => '"'. file_create_url('images/somefile.jpg', array('absolute' => $absolute )) .'"', // double quotes
+    );
+    $this->CompareSnippets($snippets);
+  }
+  
+  /**
+  * test_pathfilter
+  * 
+  * TODO: Write a description of the tests!
+  **/
+  function testFilesAbsolute() {
+    global $base_url;
+    variable_set('pathfilter_link_type', 'absolute');
+    
+    $snippets = array(
+      "'files:images/somefile.jpg'" => file_create_url('images/somefile.jpg', array('absolute' => TRUE )) ."'", // single quotes
+      '"files:images/somefile.jpg"' => file_create_url('images/somefile.jpg', array('absolute' => TRUE )) .'"', // double quotes
+    );
+    $this->CompareSnippets($snippets);
+  }
+  
+  
+  /**
+  * test_pathfilter
+  * 
+  * TODO: Write a description of the tests!
+  **/
+  function testFilesInvalidMatches() {
+    $snippets = array(
+      '"internal:admin/user\''              => PATHFILTER_NOT_OK, // mismatch quotes
+      '\'internal:admin/user"'              => PATHFILTER_NOT_OK, // mismatch quotes
+      '"files::images/somefile.jpg"'        => PATHFILTER_NOT_OK, // double colon
+      '"files:images/somefile.jpg'."'"      => PATHFILTER_NOT_OK, // mismatched quotes
+    );
+    $this->CompareSnippets($snippets);
+  }
+  
+
+    
+  /**
+  * testFid
+  * 
+  * TODO: Write a description of the tests!
+  **/
+/*  function testFid() {
+    global $base_url;
+
+    $field_name = 'field_' . strtolower($this->randomName());
+    $type = $this->drupalCreateContentType();
+    
+    $test_file = $this->getTestFile('text');
+
+    // Create a new node with the uploaded file.
+    // FIXME: How can we attach files to a node? (this was taken from filefield tests, but needs adapting)
+    $nid = $this->uploadNodeFile($test_file, 'files', $type->name);
+
+    // Check that the file exists on disk and in the database.
+    $node = node_load($nid, NULL, TRUE);
+    $node_file = $node->files[0];
+    
+    $snippets = array(
+      '"fid::1"' => PATHFILTER_NOT_OK,
+      '"fid:'. $test_file->fid .'"' => '"'. $base_url .'/'. $test_file->filepath.'"',
     );
     
     foreach ($snippets as $before => $after) {
@@ -54,5 +204,5 @@ class PathfilterTestCase extends DrupalW
         $this->assertTrue($result == $before, 'Pathfilter should not parse: '. $before.'<br/> But got: '.$result);
       }
     }
-  }  
+  }  */
 }
