Beispiel #1
0
 /**
  * Resolve this variable against a given context.
  *
  * @param Context|array $context
  *
  * @return mixed
  */
 public function resolve($context)
 {
     if ($this->lookups !== null) {
         // We're dealing with a variable that needs to be resolved
         $value = $this->resolveLookup($context);
     } else {
         // We're dealing with a literal, so it's already been "resolved"
         $value = $this->literal;
     }
     if ($this->translate) {
         if ($this->message_context) {
             return Dja::getI18n()->pgettext($this->message_context, $value);
         } else {
             return Dja::getI18n()->ugettext($value);
         }
     }
     return $value;
 }
Beispiel #2
0
 public function testTemplates()
 {
     $template_tests = self::getTemplateTests();
     $filter_tests = get_filter_tests();
     /*
      * Quickly check that we aren't accidentally using a name in both
      * template and filter tests.
      */
     $overlapping_names = array();
     $tkeys_ = array_keys($template_tests);
     foreach ($filter_tests as $name => $v) {
         if (array_key_exists($name, $tkeys_)) {
             $overlapping_names[] = $name;
         }
     }
     if (!empty($overlapping_names)) {
         throw new Exception('Duplicate test name(s): ' . join(', ', $overlapping_names));
     }
     $template_tests = array_merge($template_tests, $filter_tests);
     $tpls_ = array();
     foreach ($template_tests as $name => $t) {
         $tpls_[$name] = $t[0];
     }
     $cache_loader = setup_test_template_loader($tpls_, True);
     $failures = array();
     $tests = $template_tests;
     ksort($tests);
     // Turn TEMPLATE_DEBUG off, because tests assume that.
     $old_debug = Dja::getSetting('TEMPLATE_DEBUG');
     Dja::setSetting('TEMPLATE_DEBUG', True);
     // Set TEMPLATE_STRING_IF_INVALID to a known string.
     $old_invalid = Dja::getSetting('TEMPLATE_STRING_IF_INVALID');
     $expected_invalid_str = 'INVALID';
     // Set ALLOWED_INCLUDE_ROOTS so that ssi works.
     $old_allowed_include_roots = Dja::getSetting('ALLOWED_INCLUDE_ROOTS');
     Dja::setSetting('ALLOWED_INCLUDE_ROOTS', array(realpath(dirname(__FILE__))));
     // Warm the URL reversing cache. This ensures we don't pay the cost
     // warming the cache during one of the tests.
     Dja::getUrlDispatcher()->reverse('regressiontests.templates.views.client_action', null, array(), array('id' => 0, 'action' => "update"));
     foreach ($tests as $name => $vals) {
         if (is_array($vals[2])) {
             $normal_string_result = $vals[2][0];
             $invalid_string_result = $vals[2][1];
             if (is_array($invalid_string_result)) {
                 $expected_invalid_str = 'INVALID %s';
                 $invalid_string_result = sprintf($invalid_string_result[0], $invalid_string_result[1]);
                 DjaBase::$invalid_var_format_string = True;
             }
             if (isset($vals[2][2])) {
                 $template_debug_result = $vals[2][2];
             } else {
                 $template_debug_result = $normal_string_result;
             }
         } else {
             $normal_string_result = $vals[2];
             $invalid_string_result = $vals[2];
             $template_debug_result = $vals[2];
         }
         if (isset($vals[1]['LANGUAGE_CODE'])) {
             Dja::getI18n()->activate($vals[1]['LANGUAGE_CODE']);
         } else {
             Dja::getI18n()->activate('en-us');
         }
         foreach (array(array('', False, $normal_string_result), array($expected_invalid_str, False, $invalid_string_result), array('', True, $template_debug_result)) as $itm) {
             list($invalid_str, $template_debug, $result) = $itm;
             Dja::setSetting('TEMPLATE_STRING_IF_INVALID', $invalid_str);
             Dja::setSetting('TEMPLATE_DEBUG', $template_debug);
             foreach (array(False, True) as $is_cached) {
                 $fail_str_ = 'Template test (Cached=' . ($is_cached ? 'TRUE' : 'FALSE') . ', TEMPLATE_STRING_IF_INVALID=\'' . $invalid_str . '\', TEMPLATE_DEBUG=' . ($template_debug ? 'TRUE' : 'FALSE') . '): ' . $name . ' -- FAILED. ';
                 try {
                     try {
                         $test_template = DjaLoader::getTemplate($name);
                     } catch (ShouldNotExecuteException $e) {
                         $failures[] = $fail_str_ . 'Template loading invoked method that shouldn\'t have been invoked.';
                     }
                     try {
                         $output = self::render($test_template, $vals);
                     } catch (ShouldNotExecuteException $e) {
                         $failures[] = $fail_str_ . 'Template loading invoked method that shouldn\'t have been invoked.';
                     }
                 } catch (ContextStackException $e) {
                     $failures[] = $fail_str_ . 'Context stack was left imbalanced';
                     continue;
                 } catch (Exception $e) {
                     $exc_type = get_class($e);
                     $exc_value = $e->getMessage();
                     $exc_tb = $e->getTraceAsString();
                     if ($exc_type != $result) {
                         $tb = $exc_tb;
                         $failures[] = $fail_str_ . 'Got ' . $exc_type . ', exception: ' . $exc_value . "\n" . $tb;
                     }
                     continue;
                 }
                 if ($output != $result) {
                     $failures[] = $fail_str_ . 'Expected [' . $result . '], got [' . $output . ']';
                 }
             }
             $cache_loader->reset();
         }
         if (isset($vals[1]['LANGUAGE_CODE'])) {
             Dja::getI18n()->deactivate();
         }
         if (DjaBase::$invalid_var_format_string) {
             $expected_invalid_str = 'INVALID';
             DjaBase::$invalid_var_format_string = False;
         }
     }
     restore_template_loaders();
     Dja::getI18n()->deactivate();
     Dja::setSetting('TEMPLATE_STRING_IF_INVALID', $old_invalid);
     Dja::setSetting('TEMPLATE_DEBUG', $old_debug);
     Dja::setSetting('ALLOWED_INCLUDE_ROOTS', $old_allowed_include_roots);
     $sep_ = str_pad('', 70, '-');
     $this->assertEquals(array(), $failures, "Tests failed:\n{$sep_}\n" . join("\n{$sep_}\n", $failures));
 }
Beispiel #3
0
 public static function add_truncation_text($text, $truncate = null)
 {
     if ($truncate === null) {
         $truncate = Dja::getI18n()->pgettext('String to return when truncating text', '%(truncated_text)s...');
     }
     if (strpos($truncate, '%(truncated_text)s') !== False) {
         return str_replace('%(truncated_text)s', $text, $truncate);
     }
     /*
      * The truncation text didn't contain the %(truncated_text)s string
      * replacement argument so just append it to the text.
      */
     if (py_str_ends_with($text, $truncate)) {
         // But don't append the truncation text if the current text already ends in this.
         return $text;
     }
     return $text . $truncate;
 }
Beispiel #4
0
 */
$lib->filter('default', function ($value, $arg) {
    return $value ? $value : $arg;
}, array('is_safe' => False));
$lib->filter('default_if_none', function ($value, $arg) {
    if ($value === null) {
        return $arg;
    }
    return $value;
}, array('is_safe' => False));
$lib->filter('divisibleby', function ($value, $arg) {
    return (int) $value % (int) $arg->get() == 0;
}, array('is_safe' => False));
$lib->filter('yesno', function ($value, $arg = null) {
    if ($arg === null) {
        $arg = Dja::getI18n()->ugettext('yes,no,maybe');
    }
    $bits = explode(',', $arg);
    if (count($bits) < 2) {
        return $value;
        // Invalid arg.
    }
    @(list($yes, $no, $maybe) = $bits);
    if (!$maybe) {
        // Unpack list of wrong size (no "maybe" value provided).
        $maybe = $bits[1];
    }
    if ($value === null) {
        return $maybe;
    }
    if ($value) {