r67790 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67789‎ | r67790 | r67791 >
Date:09:03, 10 June 2010
Author:daniel
Status:deferred
Tags:
Comment:
tests for WebDataTransclusionSource: decode from local file
Modified paths:
  • /trunk/extensions/DataTransclusion/WebDataTransclusionSource.php (modified) (history)
  • /trunk/extensions/DataTransclusion/tests/DataTransclusionTest.php (modified) (history)
  • /trunk/extensions/DataTransclusion/tests/makeTestData.php (added) (history)
  • /trunk/extensions/DataTransclusion/tests/test-data-name-foo.json (added) (history)
  • /trunk/extensions/DataTransclusion/tests/test-data-name-foo.pser (added) (history)
  • /trunk/extensions/DataTransclusion/tests/test-data-name-foo.wddx (added) (history)

Diff [purge]

Index: trunk/extensions/DataTransclusion/tests/DataTransclusionTest.php
@@ -340,6 +340,32 @@
341341
342342 $this->assertEquals( $err, 'test error' );
343343 $this->assertEquals( $rec['id'], 3 );
 344+
 345+ ////////////////////////
 346+ $spec['url'] = 'file://' . dirname( realpath( __FILE__ ) ) . '/test-data-{field}-{value}.pser';
 347+ $spec['dataFormat'] = 'php';
 348+ $source = new WebDataTransclusionSource( $spec );
 349+
 350+ $rec = $source->fetchRecord( 'name', 'foo' );
 351+ $this->assertEquals( $rec['id'], 3 );
 352+
 353+ ////////////////////////
 354+ $spec['url'] = 'file://' . dirname( realpath( __FILE__ ) ) . '/test-data-{field}-{value}.json';
 355+ $spec['dataFormat'] = 'json';
 356+ $source = new WebDataTransclusionSource( $spec );
 357+
 358+ $rec = $source->fetchRecord( 'name', 'foo' );
 359+ $this->assertEquals( $rec['id'], 3 );
 360+
 361+ ////////////////////////
 362+ if ( function_exists( 'wddx_unserialize' ) ) {
 363+ $spec['url'] = 'file://' . dirname( realpath( __FILE__ ) ) . '/test-data-{field}-{value}.wddx';
 364+ $spec['dataFormat'] = 'wddx';
 365+ $source = new WebDataTransclusionSource( $spec );
 366+
 367+ $rec = $source->fetchRecord( 'name', 'foo' );
 368+ $this->assertEquals( $rec['id'], 3 );
 369+ }
344370 }
345371 }
346372
Index: trunk/extensions/DataTransclusion/tests/test-data-name-foo.wddx
@@ -0,0 +1 @@
 2+<wddxPacket version='1.0'><header/><data><struct><var name='response'><struct><var name='content'><struct><var name='foo'><struct><var name='name'><string>foo</string></var><var name='id'><number>3</number></var><var name='info'><string>test 1</string></var></struct></var></struct></var></struct></var></struct></data></wddxPacket>
\ No newline at end of file
Index: trunk/extensions/DataTransclusion/tests/makeTestData.php
@@ -0,0 +1,15 @@
 2+<?php
 3+
 4+$dir = dirname( __FILE__ );
 5+
 6+$rec = array( "name" => "foo", "id" => 3, "info" => 'test 1' );
 7+
 8+$data = array( 'response' => array(
 9+ 'content' => array(
 10+ 'foo' => $rec
 11+ )
 12+ ) );
 13+
 14+file_put_contents( "$dir/test-data-name-foo.pser", serialize( $data ) );
 15+file_put_contents( "$dir/test-data-name-foo.json", json_encode( $data ) );
 16+file_put_contents( "$dir/test-data-name-foo.wddx", wddx_serialize_value( $data ) );
\ No newline at end of file
Index: trunk/extensions/DataTransclusion/tests/test-data-name-foo.json
@@ -0,0 +1 @@
 2+{"response":{"content":{"foo":{"name":"foo","id":3,"info":"test 1"}}}}
\ No newline at end of file
Index: trunk/extensions/DataTransclusion/tests/test-data-name-foo.pser
@@ -0,0 +1 @@
 2+a:1:{s:8:"response";a:1:{s:7:"content";a:1:{s:3:"foo";a:3:{s:4:"name";s:3:"foo";s:2:"id";i:3;s:4:"info";s:6:"test 1";}}}}
\ No newline at end of file
Index: trunk/extensions/DataTransclusion/WebDataTransclusionSource.php
@@ -22,7 +22,9 @@
2323 * WebDataTransclusionSource accepts some additional options
2424 *
2525 * * $spec['url']: base URL for building urls for retrieving individual records.
26 - * The key/value pair is appended to the URL as a regular URL
 26+ * If the URL contains the placeholder {field} and/or {value}, these
 27+ * get replaced by the field name resp. the field value.
 28+ * Otherwise, the key/value pair is appended to the URL as a regular URL
2729 * parameter (preceeded by ? or &, as appropriate). For more
2830 * complex rules for building the url, override getRecordURL(). REQUIRED.
2931 * * $spec['dataFormat']: Serialization format returned from the web service.
@@ -74,7 +76,7 @@
7577 }
7678
7779 public function fetchRecord( $field, $value ) {
78 - $raw = $this->loadRecordData( $field, $value ); // TESTME
 80+ $raw = $this->loadRecordData( $field, $value );
7981 if ( !$raw ) {
8082 wfDebugLog( 'DataTransclusion', "failed to fetch data for $field=$value\n" );
8183 return false;
@@ -105,16 +107,21 @@
106108 public function getRecordURL( $field, $value ) {
107109 $u = $this->url;
108110
109 - if ( strpos( $u, '?' ) === false ) {
110 - $u .= '?';
 111+ if ( strpos( $u, '{field}' ) !== false || strpos( $u, '{value}' ) !== false ) {
 112+ $u = str_replace( '{field}', urlencode( $field ), $u );
 113+ $u = str_replace( '{value}', urlencode( $value ), $u );
111114 } else {
112 - $u .= '&';
 115+ if ( strpos( $u, '?' ) === false ) {
 116+ $u .= '?';
 117+ } else {
 118+ $u .= '&';
 119+ }
 120+
 121+ $u .= urlencode( $field );
 122+ $u .= '=';
 123+ $u .= urlencode( $value );
113124 }
114125
115 - $u .= $field;
116 - $u .= '=';
117 - $u .= urlencode( $value );
118 -
119126 return $u;
120127 }
121128
@@ -124,7 +131,7 @@
125132 if ( preg_match( '!^https?://!', $u ) ) {
126133 $raw = Http::get( $u, $this->timeout, $this->httpOptions );
127134 } else {
128 - $raw = file_get_contents( $u ); // TESTME
 135+ $raw = file_get_contents( $u );
129136 }
130137
131138 if ( $raw ) {
@@ -137,15 +144,15 @@
138145 }
139146
140147 public function decodeData( $raw, $format = 'php' ) {
141 - if ( $format == 'json' ) {
142 - return FormatJson::decode( $raw, true ); // TESTME
 148+ if ( $format == 'json' || $format == 'js' ) {
 149+ return FormatJson::decode( $raw, true );
143150 }
144151
145152 if ( $format == 'wddx' ) {
146 - return wddx_unserialize( $raw ); // TESTME
 153+ return wddx_unserialize( $raw );
147154 }
148155
149 - if ( $format == 'php' ) {
 156+ if ( $format == 'php' || $format == 'pser' ) {
150157 return unserialize( $raw );
151158 }
152159

Status & tagging log