Exemplo n.º 1
0
 /**
  * @testdox Filters work
  * @dataProvider getData
  */
 public function testFilters($filter, $original, $expected, $logs = [], $setup = null)
 {
     $this->configurator->tags->add('FOO')->attributes->add('foo')->filterChain->append($filter);
     if (isset($setup)) {
         $setup($this->configurator);
     }
     $config = $this->configurator->asConfig();
     ConfigHelper::filterVariants($config);
     $logger = new Logger();
     $parser = new ReflectionClass('s9e\\TextFormatter\\Parser');
     $method = $parser->getMethod('executeFilter');
     $method->setAccessible(true);
     $actual = $method->invoke(null, $config['tags']['FOO']['attributes']['foo']['filterChain'][0], ['attrName' => 'foo', 'attrValue' => $original, 'logger' => $logger, 'registeredVars' => $config['registeredVars']]);
     $this->assertSame($expected, $actual);
     if ($logs instanceof Closure) {
         $logs = $logs();
     }
     $this->assertSame($logs, $logger->get(), "Logs don't match");
 }
Exemplo n.º 2
0
 /**
  * Filter the value used in a [size] BBCode
  *
  * @see bbcode_firstpass::bbcode_size()
  *
  * @param  string  $size     Original size
  * @param  integer $max_size Maximum allowed size
  * @param  Logger  $logger
  * @return mixed             Original value if valid, FALSE otherwise
  */
 public static function filter_font_size($size, $max_size, Logger $logger)
 {
     if ($max_size && $size > $max_size) {
         $logger->err('MAX_FONT_SIZE_EXCEEDED', array('max_size' => $max_size));
         return false;
     }
     if ($size < 1) {
         return false;
     }
     return $size;
 }
Exemplo n.º 3
0
 /**
  * @testdox clear() empties the logs
  */
 public function testClear()
 {
     $logger = new Logger();
     $logger->debug('Hello');
     $logger->clear();
     $logger->debug('Hi');
     $this->assertSame([['debug', 'Hi', []]], $logger->get());
 }
Exemplo n.º 4
0
 public static function filterAttributes(Tag $tag, array $tagConfig, array $registeredVars, Logger $logger)
 {
     if (empty($tagConfig['attributes'])) {
         $tag->setAttributes(array());
         return \true;
     }
     foreach ($tagConfig['attributes'] as $attrName => $attrConfig) {
         if (isset($attrConfig['generator'])) {
             $tag->setAttribute($attrName, self::executeFilter($attrConfig['generator'], array('attrName' => $attrName, 'logger' => $logger, 'registeredVars' => $registeredVars)));
         }
     }
     foreach ($tag->getAttributes() as $attrName => $attrValue) {
         if (!isset($tagConfig['attributes'][$attrName])) {
             $tag->removeAttribute($attrName);
             continue;
         }
         $attrConfig = $tagConfig['attributes'][$attrName];
         if (!isset($attrConfig['filterChain'])) {
             continue;
         }
         $logger->setAttribute($attrName);
         foreach ($attrConfig['filterChain'] as $filter) {
             $attrValue = self::executeFilter($filter, array('attrName' => $attrName, 'attrValue' => $attrValue, 'logger' => $logger, 'registeredVars' => $registeredVars));
             if ($attrValue === \false) {
                 $tag->removeAttribute($attrName);
                 break;
             }
         }
         if ($attrValue !== \false) {
             $tag->setAttribute($attrName, $attrValue);
         }
         $logger->unsetAttribute();
     }
     foreach ($tagConfig['attributes'] as $attrName => $attrConfig) {
         if (!$tag->hasAttribute($attrName)) {
             if (isset($attrConfig['defaultValue'])) {
                 $tag->setAttribute($attrName, $attrConfig['defaultValue']);
             } elseif (!empty($attrConfig['required'])) {
                 return \false;
             }
         }
     }
     return \true;
 }
Exemplo n.º 5
0
 /**
  * Filter the attributes of given tag
  *
  * @private
  *
  * @param  Tag    $tag            Tag being checked
  * @param  array  $tagConfig      Tag's config
  * @param  array  $registeredVars Array of registered vars for use in attribute filters
  * @param  Logger $logger         This parser's Logger instance
  * @return bool                   Whether the whole attribute set is valid
  */
 public static function filterAttributes(Tag $tag, array $tagConfig, array $registeredVars, Logger $logger)
 {
     if (empty($tagConfig['attributes'])) {
         $tag->setAttributes([]);
         return true;
     }
     // Generate values for attributes with a generator set
     foreach ($tagConfig['attributes'] as $attrName => $attrConfig) {
         if (isset($attrConfig['generator'])) {
             $tag->setAttribute($attrName, self::executeFilter($attrConfig['generator'], ['attrName' => $attrName, 'logger' => $logger, 'registeredVars' => $registeredVars]));
         }
     }
     // Filter and remove invalid attributes
     foreach ($tag->getAttributes() as $attrName => $attrValue) {
         // Test whether this attribute exists and remove it if it doesn't
         if (!isset($tagConfig['attributes'][$attrName])) {
             $tag->removeAttribute($attrName);
             continue;
         }
         $attrConfig = $tagConfig['attributes'][$attrName];
         // Test whether this attribute has a filterChain
         if (!isset($attrConfig['filterChain'])) {
             continue;
         }
         // Record the name of the attribute being filtered into the logger
         $logger->setAttribute($attrName);
         foreach ($attrConfig['filterChain'] as $filter) {
             $attrValue = self::executeFilter($filter, ['attrName' => $attrName, 'attrValue' => $attrValue, 'logger' => $logger, 'registeredVars' => $registeredVars]);
             if ($attrValue === false) {
                 $tag->removeAttribute($attrName);
                 break;
             }
         }
         // Update the attribute value if it's valid
         if ($attrValue !== false) {
             $tag->setAttribute($attrName, $attrValue);
         }
         // Remove the attribute's name from the logger
         $logger->unsetAttribute();
     }
     // Iterate over the attribute definitions to handle missing attributes
     foreach ($tagConfig['attributes'] as $attrName => $attrConfig) {
         // Test whether this attribute is missing
         if (!$tag->hasAttribute($attrName)) {
             if (isset($attrConfig['defaultValue'])) {
                 // Use the attribute's default value
                 $tag->setAttribute($attrName, $attrConfig['defaultValue']);
             } elseif (!empty($attrConfig['required'])) {
                 // This attribute is missing, has no default value and is required, which means
                 // the attribute set is invalid
                 return false;
             }
         }
     }
     return true;
 }