Permalink
Browse files

Organizes the php scripts used for testing better, so that the whole …

…logic of a unit, server-side and client-side, is contained within the unit itself. Nearly all ajax unit tests take advantage of the new 'framework'. Lots of files got deleted because they became redundant or weren't used anymore.
  • Loading branch information...
1 parent 3ab2634 commit 228ab3ddae527f72cc3122a1c6115d7718bcfd57 @jaubourg jaubourg committed Dec 4, 2012
View
@@ -41,11 +41,15 @@
"ajaxTest": true,
"testIframe": true,
"testIframeWithCallback": true,
+ "createComplexHTML": true,
@Krinkle
Krinkle Dec 5, 2012 Member

Mixed tabs and spaces..

"createDashboardXML": true,
+ "createWithFriesXML": true,
"createXMLFragment": true,
"moduleTeardown": true,
+ "testBar": true,
"testFoo": true,
- "url": true,
+ "url": true,
+ "service": true,
"t": true,
"q": true,
"amdDefined": true,
@@ -0,0 +1,43 @@
+<?php
+
+ $requestArray = "REQUEST";
+
+ if ( isset( $_REQUEST["requestArray"] ) ) {
+ $requestArray = $_REQUEST["requestArray"];
+ }
+
+ $requestArray =& ${"_$requestArray"};
@Krinkle
Krinkle Dec 5, 2012 Member

This is a horrible way to simply switch between GET and POST variable retrieval.

@jaubourg
jaubourg Dec 5, 2012 Member

OK, then I'll use an array, I see the security concern, I was deep into the tests trying to find a way to switch.

+
+ $response = array(
+ "status" => "200",
+ "statusText" => "",
+ "contentType" => "text/plain",
+ "content" => "",
+ "callback" => "",
+ "delay" => 0
+ );
+
+ foreach( $response as $field => &$value ) {
+ if ( isset( $requestArray[ $field ] ) ) {
+ $value = $requestArray[ $field ];
+ }
+ }
+
+ if ( is_array( $response["content"] ) ) {
+ $response["content"] = http_build_query( $response["content"] );
+ }
+
+ if ( !$response["callback"] && preg_match( '/index.php\/([^\/\?&]+)/', $_SERVER["REQUEST_URI"], $match ) ) {
+ $response["callback"] = $match[ 1 ];
+ }
+
+ header("HTTP/1.1 $response[status] $response[statusText]");
@Krinkle
Krinkle Dec 5, 2012 Member
PHP Notice:  Use of undefined constant status 
PHP Notice:  Use of undefined constant statusText 
PHP Notice:  Use of undefined constant contentType
PHP Notice:  Use of undefined constant content

Keys have to be quoted, even in PHP. Please enable error_reporting during development.

@jaubourg
jaubourg Dec 5, 2012 Member

Got you. No idea why my installation doesn't show errors though. It should.

+ header("Content-Type: $response[contentType]");
+
+ if ( $response["delay"] ) {
+ sleep( $response["delay"] );
+ }
+
+ echo $response["callback"]
+ ? "$response[callback](" . json_encode("$response[content]") . ");"
@Krinkle
Krinkle Dec 5, 2012 Member

Why is $response['content'] embedded in a string?

@jaubourg
jaubourg Dec 5, 2012 Member

Because I thought I didn't need the quotes, which I no know I do.

+ : "$response[content]";
@@ -0,0 +1,30 @@
+<?php
+$headers = array(
+
+ "If-Modified-Since" => array(
+ "request" => "HTTP_IF_MODIFIED_SINCE",
+ "response" => "Last-Modified",
+ ),
+ "If-None-Match" => array(
+ "request" => "HTTP_IF_NONE_MATCH",
+ "response" => "Etag",
+ ),
+
+);
+
+$header = $_REQUEST["header"];
+$value = $_REQUEST["value"];
+
+if ( $header === "If-None-Match" ) {
+ $value = md5( $value );
+}
+
+$headers = $headers[ $header ];
+
+$requestHeader = isset( $_SERVER[ $headers["request"] ] ) ? stripslashes($_SERVER[ $headers["request"] ]) : false;
@Krinkle
Krinkle Dec 5, 2012 Member

Why stripslashes()?

+if ( $requestHeader === $value ) {
+ header("HTTP/1.0 304 Not Modified");
+} else {
+ header("$headers[response]: $value");
@Krinkle
Krinkle Dec 5, 2012 Member
PHP Notice:  Use of undefined constant response 
+ echo $requestHeader ? "OK: $value": "FAIL";
+}
@@ -0,0 +1,12 @@
+<?php
+
+$headers = array();
+
+foreach( $_SERVER as $key => $value ) {
+ $key = str_replace( "_" , "-" , substr($key,0,5) == "HTTP_" ? substr($key,5) : $key );
+ $headers[ $key ] = $value;
+}
+
+foreach( explode( "," , $_GET["headers"] ) as $key ) {
+ echo "$key: " . @$headers[ strtoupper( $key ) ] . "\n";
+}
@@ -0,0 +1,5 @@
+<?php
+
+foreach( $_REQUEST as $header => $value ) {
+ @header("$header: $value");
@Krinkle
Krinkle Dec 5, 2012 Member

Why error @ error suppression?

@jaubourg
jaubourg Dec 5, 2012 Member

That was reminiscent of the old code.

+}
@@ -1,4 +0,0 @@
-<?php header("Content-type: atom+xml") ?>
-<root>
- <element />
-</root>
@@ -1 +0,0 @@
-undefined();
@@ -1 +0,0 @@
-{bad: toTheBone}
@@ -1,10 +0,0 @@
-<script>
-<!--
-ok( true, "script within html comments executed" );
--->
-</script>
-<script>
-<![CDATA[
-ok( true, "script within CDATA executed" );
-]]>
-</script>
@@ -1,11 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<dashboard>
- <locations class="foo">
- <location for="bar" checked="different">
- <infowindowtab>
- <tab title="Location"><![CDATA[blabla]]></tab>
- <tab title="Users"><![CDATA[blublu]]></tab>
- </infowindowtab>
- </location>
- </locations>
-</dashboard>
@@ -1 +0,0 @@
-<?php echo file_get_contents('php://input'); ?>
@@ -1 +0,0 @@
-<?php echo $_SERVER['QUERY_STRING']; ?>
@@ -1,5 +0,0 @@
-<?php
-
-header("HTTP/1.0 400 Bad Request");
-
-echo "plain text message";
View
@@ -1,21 +0,0 @@
-<?php
-error_reporting(0);
-
-$ts = $_REQUEST['ts'];
-$etag = md5($ts);
-
-$ifNoneMatch = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false;
-if ($ifNoneMatch == $etag) {
- header('HTTP/1.0 304 Not Modified');
- die; // stop processing
-}
-
-header("Etag: " . $etag);
-
-if ( $ifNoneMatch ) {
- echo "OK: " . $etag;
-} else {
- echo "FAIL";
-}
-
-?>
@@ -1 +0,0 @@
-ok( "<?php echo $_SERVER['REQUEST_METHOD'] ?>" === "GET", "request method is <?php echo $_SERVER['REQUEST_METHOD'] ?>" );
View
@@ -1,18 +0,0 @@
-<?php
-
-header( "Sample-Header: Hello World" );
-header( "Empty-Header: " );
-header( "Sample-Header2: Hello World 2" );
-
-$headers = array();
-
-foreach( $_SERVER as $key => $value ) {
-
- $key = str_replace( "_" , "-" , substr( $key , 0 , 5 ) == "HTTP_" ? substr( $key , 5 ) : $key );
- $headers[ $key ] = $value;
-
-}
-
-foreach( explode( "_" , $_GET[ "keys" ] ) as $key ) {
- echo "$key: " . @$headers[ strtoupper( $key ) ] . "\n";
-}
@@ -1,20 +0,0 @@
-<?php
-error_reporting(0);
-
-$ts = $_REQUEST['ts'];
-
-$ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
-if ($ifModifiedSince == $ts) {
- header('HTTP/1.0 304 Not Modified');
- die; // stop processing
-}
-
-header("Last-Modified: " . $ts);
-
-if ( $ifModifiedSince ) {
- echo "OK: " . $ts;
-} else {
- echo "FAIL";
-}
-
-?>
View
@@ -1,13 +0,0 @@
-<?php
-error_reporting(0);
-if ( $_REQUEST['header'] ) {
- header("Content-type: application/json");
-}
-
-$json = $_REQUEST['json'];
-if($json) {
- echo '[ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ]';
-} else {
- echo '{ "data": {"lang": "en", "length": 25} }';
-}
-?>
@@ -1 +0,0 @@
-{ "data": {"lang": "en", "length": 25} }
View
@@ -1,14 +0,0 @@
-<?php
-error_reporting(0);
-$callback = $_REQUEST['callback'];
-if ( ! $callback ) {
- $callback = explode("?",end(explode("/",$_SERVER['REQUEST_URI'])));
- $callback = $callback[0];
-}
-$json = $_REQUEST['json'];
-if($json) {
- echo $callback . '([ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ])';
-} else {
- echo $callback . '({ "data": {"lang": "en", "length": 25} })';
-}
-?>
View
@@ -1 +0,0 @@
-ERROR <script type="text/javascript">ok( true, "name.html retrieved" );</script>
View
@@ -1,24 +0,0 @@
-<?php
-error_reporting(0);
-$wait = $_REQUEST['wait'];
-if($wait) {
- sleep($wait);
-}
-$xml = $_REQUEST['xml'];
-if($xml) {
- header("Content-type: text/xml");
- $result = ($xml == "5-2") ? "3" : "?";
- echo "<math><calculation>$xml</calculation><result>$result</result></math>";
- die();
-}
-$name = $_REQUEST['name'];
-if($name == 'foo') {
- echo "bar";
- die();
-} else if($name == 'peter') {
- echo "pan";
- die();
-}
-
-echo 'ERROR <script type="text/javascript">ok( true, "name.php executed" );</script>';
-?>
@@ -1,12 +0,0 @@
-<div id="post">
-<?php
- foreach( $_POST as $key=>$value )
- echo "<b id='$key'>$value</b>";
-?>
-</div>
-<div id="get">
-<?php
- foreach( $_GET as $key=>$value )
- echo "<b id='$key'>$value</b>";
-?>
-</div>
View
@@ -1,11 +0,0 @@
-<?php
-error_reporting(0);
-if ( $_REQUEST['header'] ) {
- if ( $_REQUEST['header'] == "ecma" ) {
- header("Content-type: application/ecmascript");
- } else {
- header("Content-type: text/javascript");
- }
-}
-?>
-ok( true, "Script executed correctly." );
@@ -1,5 +0,0 @@
-<?php
-
-header( "HTTP/1.0 $_GET[status] $_GET[text]" );
-
-?>
View
@@ -1,7 +0,0 @@
-html text<br/>
-<script type="text/javascript">/* <![CDATA[ */
-testFoo = "foo"; jQuery('#foo').html('foo');
-ok( true, "test.html executed" );
-/* ]]> */</script>
-<script src="data/test.js"></script>
-blabla
View
@@ -1,3 +0,0 @@
-var testBar = "bar";
-jQuery('#ap').html('bar');
-ok( true, "test.js executed");
View
@@ -1,7 +0,0 @@
-html text<br/>
-<script type="text/javascript">/* <![CDATA[ */
-testFoo = "foo"; jQuery('#foo').html('foo');
-ok( true, "test.php executed" );
-/* ]]> */</script>
-<script src="data/test.js?<?php srand(); echo time() . '' . rand(); ?>"></script>
-blabla
@@ -1,5 +0,0 @@
-<script type="text/javascript">
-var testFoo = "foo";
-jQuery('#foo').html('foo');
-ok( true, "test2.html executed" );
-</script>
@@ -1,3 +0,0 @@
-<div class="user">This is a user</div>
-<div class="user">This is a user</div>
-<div class="teacher">This is a teacher</div>
Oops, something went wrong.

7 comments on commit 228ab3d

@jaubourg
Member

@Krinkle seems like we have 12 failures on all browsers in ajax units... thing is I ran those test locally against all browsers so there must be some config discrepancy. Gotta get some sleep right now, but if you have manage to guess what's going on, I'd be happy to adapt the tests to testswarm's configuration if needs be later today ;)

@Krinkle
Member
Krinkle commented on 228ab3d Dec 5, 2012

Please some consistency. echo, echo/, echo/index.php. What's it gonna be?

I'd recommend either being completely explicit everywhere (echo/index.php) or abstracting it (calling service() only with a symbolic name and mapping those to a file by appending .php). For example, rename echo/index.php to echo.php. And call service("echo"), which then appends .php.

Depending on which web server (Apache, Nginx, Nodejs, ..) and configuration therefor, various of these may fail (12 fail on jQuery servers).

There various random factors being relied on:

  • accepting directory lookups without trailing slash
  • assuming that even if that is supported, that it does not use a redirect (in which case the AJAX request would get a 301 response)
  • using index.php as the (or one of) default directory index files
    etc.

In case of TestSwarm servers, it does support directory lookups without trailing slash but 301 redirects, causing some of the requests to fail when they get 301 instead of whatever response is expected.

@Krinkle
Member
Krinkle commented on 228ab3d Dec 5, 2012

There is various other issues with the PHP code that are imho unacceptable (we do execute this, server side, on production servers!). Please do this from a PR next time instead of pushing straight into master.

@Krinkle
Member
Krinkle commented on 228ab3d Dec 5, 2012

228ab3d (by @gibson042) fixes a few of the path errors but from running it locally and by actually looking at the errors on TestSwarm I see that that was only a small part of the problem. I can reproduce the majority of the issues locally regardless of any path format.

There seem to be some invalid XML responses and stuff. Not something I can imagine would make any difference on whatever web server or browser.

And there are the style/security issues with PHP code mentioned earlier (though those don't affect the tests). I don't have time for this right now, as in, we can't have this in master taking over other people's priority all of a sudden.

I recommend reverting these 2 commits out of master ASAP. Then re-submit it as a pull request to go through further review and iteration until it passes the tests.

@jaubourg
Member

@Krinkle, there are only 12 tests not passing. I'd prefer we fix the issue on master and be done with it rather than revert/PR/commit... We can fix all the issues here with a simple commit.

If you prefer it consistent, then we can make sure the paths are always correctly formatted. I just assumed that 301 were followed and that data just weren't rePOSTed again after the redirection (hence the differences between echo, echo/, ...). I can as easily switch to a pure file.php approach, that like 2 minutes of work.

I see the request array switching as a security risk, is there any other security issue I didn't see? Also, where is the style guide for the php code? Is there any? I tried to stay consistent with the Core style guide as I didn't know of any specific to PHP.

@scottgonzalez
Member

Also, where is the style guide for the php code? Is there any? I tried to stay consistent with the Core style guide as I didn't know of any specific to PHP.

We should write one (or at least write the differences between PHP and JS). Basically, just follow the core style guide but use single quotes instead of double quotes unless you need string interpolation.

@jaubourg
Member

#1058 as per discussion on IRC.

Please sign in to comment.