Skip to content

Commit be33cfe

Browse files
committed
#73 Add unit tests for JsonP & JsonXss
1 parent 8a8ce20 commit be33cfe

File tree

5 files changed

+283
-1
lines changed

5 files changed

+283
-1
lines changed

main/UI/View/JsonPView.class.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2012 by Georgiy T. Kutsurua *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
/**
13+
* @ingroup Flow
14+
**/
15+
16+
class JsonPView extends JsonView
17+
{
18+
/**
19+
* @static
20+
* @return JsonPView
21+
*/
22+
public static function create()
23+
{
24+
return new self();
25+
}
26+
27+
/**
28+
* Callback function name
29+
* @see http://en.wikipedia.org/wiki/JSONP
30+
* @var string
31+
*/
32+
protected $callback = null;
33+
34+
/**
35+
* @param mixed $callback
36+
* @return JsonPView
37+
*/
38+
public function setCallback($callback)
39+
{
40+
$this->callback = $callback;
41+
42+
return $this;
43+
}
44+
45+
/**
46+
* @param Model $model
47+
* @return string
48+
*/
49+
public function toString(/* Model */ $model = null)
50+
{
51+
$callback = null;
52+
53+
if(is_scalar($this->callback))
54+
$callback = $this->callback;
55+
elseif($this->callback instanceof Stringable)
56+
$callback = $this->callback->toString();
57+
else
58+
throw new WrongArgumentException('undefined type of callback, gived "'.gettype($this->callback).'"');
59+
60+
Assert::isNotEmpty($callback, 'callback can not be empty!');
61+
62+
if(!preg_match('/^[\$A-Z_][0-9A-Z_\$]*$/i', $callback))
63+
throw new WrongArgumentException('invalid function name, you should set valid javascript function name! gived "'.$callback.'"');
64+
65+
$json = parent::toString($model);
66+
67+
return $callback.'('.$json.');';
68+
}
69+
70+
}

main/UI/View/JsonView.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* @ingroup Flow
1414
**/
15-
final class JsonView implements View, Stringable
15+
class JsonView implements View, Stringable
1616
{
1717
protected $options = 0;
1818

main/UI/View/JsonXssView.class.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2012 by Georgiy T. Kutsurua *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
/**
13+
* @ingroup Flow
14+
**/
15+
16+
class JsonXssView extends JsonPView
17+
{
18+
19+
/**
20+
* @static
21+
* @return JsonXssView
22+
*/
23+
public static function create()
24+
{
25+
return new self();
26+
}
27+
28+
/**
29+
* @param Model $model
30+
* @return string
31+
*/
32+
public function toString(/* Model */ $model = null)
33+
{
34+
/*
35+
* Escaping warning datas
36+
*/
37+
$this->setHexAmp(true);
38+
$this->setHexApos(true);
39+
$this->setHexQuot(true);
40+
$this->setHexTag(true);
41+
42+
$jsonp = parent::toString($model);
43+
44+
$jsonp = str_ireplace(
45+
array('u0022', 'u0027'),
46+
array('\u0022', '\u0027'),
47+
$jsonp
48+
);
49+
50+
$result = '<script type="text/javascript">'."\n";
51+
$result.="\t".$jsonp."\n";
52+
$result.='</script>'."\n";
53+
54+
return $result;
55+
}
56+
57+
}

test/main/JsonPViewTest.class.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2012 by Georgiy T. Kutsurua *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
final class JsonPViewTest extends TestCase
13+
{
14+
protected $array = array('<foo>',"'bar'",'"baz"','&blong&');
15+
16+
17+
public function testMain()
18+
{
19+
$this->execCallback('myCallback');
20+
21+
try{
22+
$this->execCallback(''); // empty js callback function name
23+
24+
$this->fail('empty callback javascript function name expected!');
25+
} catch(WrongArgumentException $e) {}
26+
27+
try{
28+
$this->execCallback('34_callback'); // invalid javascript function name
29+
30+
$this->fail('invalid javascript function name expected!');
31+
} catch(WrongArgumentException $e) {}
32+
33+
}
34+
35+
protected function execCallback($callback)
36+
{
37+
Assert::isScalar($callback);
38+
39+
$model = Model::create()->set('array', $this->array);
40+
$data = array('array' => $this->array);
41+
42+
//setup
43+
$view = JsonPView::create()->setCallback($callback);
44+
45+
//execution and check
46+
$this->assertEquals(
47+
$callback.'('.json_encode(
48+
$data
49+
).');',
50+
$view->toString($model)
51+
);
52+
53+
//setup from stringable object
54+
$view = JsonPView::create()->setCallback(
55+
SimpleStringableObject::create()->setString($callback)
56+
);
57+
58+
//execution and check
59+
$this->assertEquals(
60+
$callback.'('.json_encode(
61+
$data
62+
).');',
63+
$view->toString($model)
64+
);
65+
66+
}
67+
68+
}
69+
70+
class SimpleStringableObject implements Stringable
71+
{
72+
protected $string = null;
73+
74+
75+
/**
76+
* @static
77+
* @return SimpleStringableObject
78+
*/
79+
public static function create()
80+
{
81+
return new self();
82+
}
83+
84+
/**
85+
* @param $value
86+
* @return SimpleStringableObject
87+
*/
88+
public function setString($value)
89+
{
90+
Assert::isString($value);
91+
92+
$this->string = $value;
93+
94+
return $this;
95+
}
96+
97+
/**
98+
* @return str
99+
*/
100+
public function toString()
101+
{
102+
return $this->string;
103+
}
104+
}
105+
?>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2012 by Georgiy T. Kutsurua *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
final class JsonXssViewTest extends TestCase
13+
{
14+
protected $array = array('<foo>',"'bar'",'"baz"','&blong&');
15+
16+
public function testMain()
17+
{
18+
$callback = 'myCallback';
19+
20+
$model = Model::create()->set('array', $this->array);
21+
$data = array('array' => $this->array);
22+
23+
//setup
24+
$view = JsonXssView::create()->setCallback($callback);
25+
26+
//execution and check
27+
$this->assertEquals(
28+
'<script type="text/javascript">'."\n".
29+
"\t".$callback.'('.
30+
str_ireplace(
31+
array('u0022', 'u0027'),
32+
array('\u0022', '\u0027'),
33+
json_encode(
34+
$data,
35+
JSON_HEX_AMP |
36+
JSON_HEX_APOS |
37+
JSON_HEX_QUOT |
38+
JSON_HEX_TAG
39+
)
40+
).
41+
');'."\n".
42+
'</script>'."\n",
43+
$view->toString($model)
44+
);
45+
46+
}
47+
48+
}
49+
50+
?>

0 commit comments

Comments
 (0)