-
Notifications
You must be signed in to change notification settings - Fork 0
Fix test failures: register cypher_raw magic word and handle final class in tests #465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df42128
Initial plan
Copilot 6cde9be
Add cypher_raw parser function implementation and tests
Copilot ecd9bb1
Address code review: add i18n and JSON encoding error handling
Copilot 1e01594
Use single quotes with escaped double quotes for HTML strings
Copilot 4d065ea
Add cypher_raw parser function demo page to DemoData
Copilot a643456
Revise cypher_raw parser function description
JeroenDeDauw e8bea06
Fix test failures: add magic words and fix final class mocking
Copilot 55447ce
Fix test stubs to properly return SummarizedResult type
Copilot eff1f32
Use willReturnCallback with anonymous class to avoid stubbing final c…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| This page demonstrates the <code>cypher_raw</code> parser function for executing Cypher queries against the Neo4j graph database. | ||
|
|
||
| This is a '''demo feature''' for the NeoWiki proof of concept demo. It will not be present as-is in a production version. | ||
|
|
||
| == Basic Query Example == | ||
|
|
||
| Query all companies in the database: | ||
|
|
||
| {{#cypher_raw: MATCH (n:Company) RETURN n.label, n.id LIMIT 5}} | ||
|
|
||
| == Query with Filtering == | ||
|
|
||
| Find companies founded after 2018: | ||
|
|
||
| {{#cypher_raw: MATCH (n:Company) WHERE n.founded_at > 2018 RETURN n.label, n.founded_at}} | ||
|
|
||
| == Query with Relations == | ||
|
|
||
| Find companies and their products: | ||
|
|
||
| {{#cypher_raw: MATCH (company:Company)-[:Has_product]->(product:Product) RETURN company.label, product.label LIMIT 10}} | ||
|
|
||
| == Notes == | ||
|
|
||
| * Only read-only queries are allowed. Write operations like <code>CREATE</code>, <code>SET</code>, <code>DELETE</code> are rejected. | ||
| * Query results are displayed as formatted JSON. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?php | ||
|
|
||
| $magicWords = []; | ||
|
|
||
| $magicWords['en'] = [ | ||
| 'cypher_raw' => [ 0, 'cypher_raw' ], | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace ProfessionalWiki\NeoWiki\EntryPoints; | ||
|
|
||
| use Exception; | ||
| use MediaWiki\Parser\Parser; | ||
| use ProfessionalWiki\NeoWiki\CypherQueryFilter; | ||
| use ProfessionalWiki\NeoWiki\Persistence\QueryEngine; | ||
| use RuntimeException; | ||
|
|
||
| class CypherRawParserFunction { | ||
|
|
||
| public function __construct( | ||
| private readonly QueryEngine $queryEngine, | ||
| private readonly CypherQueryFilter $queryFilter | ||
| ) { | ||
| } | ||
|
|
||
| public function handle( Parser $parser, string $cypherQuery ): string { | ||
| $cypherQuery = trim( $cypherQuery ); | ||
|
|
||
| if ( $cypherQuery === '' ) { | ||
| return $this->formatError( wfMessage( 'neowiki-cypher-raw-error-empty-query' )->text() ); | ||
| } | ||
|
|
||
| if ( !$this->queryFilter->isReadQuery( $cypherQuery ) ) { | ||
| return $this->formatError( wfMessage( 'neowiki-cypher-raw-error-write-query' )->text() ); | ||
| } | ||
|
|
||
| try { | ||
| $result = $this->queryEngine->runReadQuery( $cypherQuery ); | ||
| $jsonOutput = json_encode( $result->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); | ||
|
|
||
| if ( $jsonOutput === false ) { | ||
| throw new RuntimeException( wfMessage( 'neowiki-cypher-raw-error-json-encode' )->text() ); | ||
| } | ||
|
|
||
| return $this->formatCodeBlock( $jsonOutput ); | ||
| } catch ( Exception $e ) { | ||
| return $this->formatError( wfMessage( 'neowiki-cypher-raw-error-query-failed', $e->getMessage() )->text() ); | ||
| } | ||
| } | ||
|
|
||
| private function formatCodeBlock( string $content ): string { | ||
| return '<pre><code class="json">' . "\n" . htmlspecialchars( $content ) . "\n" . '</code></pre>'; | ||
| } | ||
|
|
||
| private function formatError( string $message ): string { | ||
| return '<div class="error">' . htmlspecialchars( $message ) . '</div>'; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
tests/phpunit/EntryPoints/CypherRawParserFunctionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace ProfessionalWiki\NeoWiki\Tests\EntryPoints; | ||
|
|
||
| use Exception; | ||
| use Laudis\Neo4j\Databags\SummarizedResult; | ||
| use MediaWiki\Parser\Parser; | ||
| use PHPUnit\Framework\TestCase; | ||
| use ProfessionalWiki\NeoWiki\CypherQueryFilter; | ||
| use ProfessionalWiki\NeoWiki\EntryPoints\CypherRawParserFunction; | ||
| use ProfessionalWiki\NeoWiki\Persistence\QueryEngine; | ||
|
|
||
| /** | ||
| * @covers \ProfessionalWiki\NeoWiki\EntryPoints\CypherRawParserFunction | ||
| */ | ||
| class CypherRawParserFunctionTest extends TestCase { | ||
|
|
||
| private function createMockParser(): Parser { | ||
| return $this->createMock( Parser::class ); | ||
| } | ||
|
|
||
| private function createDummyQueryEngine(): QueryEngine { | ||
| // Create a simple mock that won't be called | ||
| return $this->createMock( QueryEngine::class ); | ||
| } | ||
|
|
||
| private function createQueryEngineWithData( array $returnData ): QueryEngine { | ||
| $queryEngine = $this->createMock( QueryEngine::class ); | ||
| $queryEngine | ||
| ->method( 'runReadQuery' ) | ||
| ->willReturnCallback( function() use ( $returnData ) { | ||
| // We need to return something that has a toArray() method | ||
| // Since SummarizedResult is final, we use an anonymous class | ||
| return new class( $returnData ) { | ||
| public function __construct( private array $data ) {} | ||
| public function toArray(): array { | ||
| return $this->data; | ||
| } | ||
| }; | ||
| } ); | ||
| return $queryEngine; | ||
| } | ||
|
|
||
| private function createQueryEngineWithException( Exception $exception ): QueryEngine { | ||
| $queryEngine = $this->createMock( QueryEngine::class ); | ||
| $queryEngine | ||
| ->method( 'runReadQuery' ) | ||
| ->willThrowException( $exception ); | ||
| return $queryEngine; | ||
| } | ||
|
|
||
| public function testEmptyQueryReturnsError(): void { | ||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createDummyQueryEngine(), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), '' ); | ||
|
|
||
| $this->assertStringContainsString( 'error', $result ); | ||
| } | ||
|
|
||
| public function testWriteQueryIsRejected(): void { | ||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createDummyQueryEngine(), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), "CREATE (n:Person {name: 'Alice'})" ); | ||
|
|
||
| $this->assertStringContainsString( 'error', $result ); | ||
| } | ||
|
|
||
| public function testValidReadQueryReturnsFormattedResult(): void { | ||
| $testData = [ | ||
| [ 'name' => 'Alice', 'age' => 30 ], | ||
| [ 'name' => 'Bob', 'age' => 25 ] | ||
| ]; | ||
|
|
||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createQueryEngineWithData( $testData ), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), 'MATCH (n:Person) RETURN n' ); | ||
|
|
||
| $this->assertStringContainsString( '<pre><code class="json">', $result ); | ||
| $this->assertStringContainsString( 'Alice', $result ); | ||
| $this->assertStringContainsString( 'Bob', $result ); | ||
| } | ||
|
|
||
| public function testQueryExecutionExceptionReturnsError(): void { | ||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createQueryEngineWithException( new Exception( 'Connection failed' ) ), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), 'MATCH (n) RETURN n' ); | ||
|
|
||
| $this->assertStringContainsString( 'error', $result ); | ||
| } | ||
|
|
||
| public function testTrimWhitespaceFromQuery(): void { | ||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createQueryEngineWithData( [] ), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), ' MATCH (n) RETURN n ' ); | ||
|
|
||
| $this->assertStringContainsString( '<pre><code class="json">', $result ); | ||
| } | ||
|
|
||
| public function testOutputIsHTMLEscaped(): void { | ||
| $testData = [ | ||
| [ 'name' => '<script>alert("xss")</script>' ] | ||
| ]; | ||
|
|
||
| $parserFunction = new CypherRawParserFunction( | ||
| $this->createQueryEngineWithData( $testData ), | ||
| new CypherQueryFilter() | ||
| ); | ||
|
|
||
| $result = $parserFunction->handle( $this->createMockParser(), 'MATCH (n) RETURN n' ); | ||
|
|
||
| $this->assertStringNotContainsString( '<script>alert', $result ); | ||
| $this->assertStringContainsString( '<script>', $result ); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked, and seems reasonable to actually check for failed json decode.