-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,119 @@ | ||
<?php | ||
|
||
// XXX: Can we autoload this somehow? | ||
require_once __DIR__ . '/PolylangUnitTestCase.php'; | ||
|
||
class PostObjectQueryTest extends PolylangUnitTestCase | ||
{ | ||
static function wpSetUpBeforeClass() | ||
{ | ||
parent::wpSetUpBeforeClass(); | ||
|
||
self::set_default_language('en_US'); | ||
self::create_language('en_US'); | ||
self::create_language('fr_FR'); | ||
self::create_language('fi'); | ||
self::create_language('de_DE_formal'); | ||
self::create_language('es_ES'); | ||
} | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function testCanHaveLanguageField() | ||
{ | ||
$post_id = $this->factory()->post->create([ | ||
'post_title' => 'Original post', | ||
'post_content' => '', | ||
'post_type' => 'post', | ||
'post_status' => 'publish', | ||
]); | ||
pll_set_post_language($post_id, 'fi'); | ||
|
||
$preview_id = $this->factory()->post->create([ | ||
'post_title' => 'Preview post', | ||
'post_content' => 'Preview Content', | ||
'post_type' => 'revision', | ||
'post_status' => 'inherit', | ||
'post_parent' => $post_id, | ||
]); | ||
pll_set_post_language($preview_id, 'en'); | ||
|
||
$query = " | ||
query Preview { | ||
post(id: \"$post_id\", idType: DATABASE_ID, asPreview: true) { | ||
title | ||
language { | ||
code | ||
} | ||
} | ||
} | ||
"; | ||
|
||
wp_set_current_user(1); | ||
$result = do_graphql_request($query); | ||
$this->assertArrayNotHasKey('errors', $result, print_r($result, true)); | ||
$this->assertEquals($result['data']['post']['language']['code'], 'EN'); | ||
} | ||
|
||
public function testCanFetchTranslatedVersions() | ||
{ | ||
$fi_post_id = wp_insert_post([ | ||
'post_title' => 'Finnish post version', | ||
'post_content' => '', | ||
'post_type' => 'post', | ||
'post_status' => 'publish', | ||
]); | ||
pll_set_post_language($fi_post_id, 'fi'); | ||
|
||
$en_post_id = wp_insert_post([ | ||
'post_title' => 'English post version', | ||
'post_content' => '', | ||
'post_type' => 'post', | ||
'post_status' => 'publish', | ||
]); | ||
pll_set_post_language($en_post_id, 'en'); | ||
|
||
pll_save_post_translations([ | ||
'en' => $en_post_id, | ||
'fi' => $fi_post_id, | ||
]); | ||
|
||
$preview_id = $this->factory()->post->create([ | ||
'post_title' => 'Preview post', | ||
'post_content' => 'Preview Content', | ||
'post_type' => 'revision', | ||
'post_status' => 'inherit', | ||
'post_parent' => $en_post_id, | ||
]); | ||
|
||
$query = " | ||
query Preview { | ||
post(id: \"$en_post_id\", idType: DATABASE_ID, asPreview: true) { | ||
title | ||
translations { | ||
title | ||
} | ||
} | ||
} | ||
"; | ||
|
||
wp_set_current_user(1); | ||
$data = do_graphql_request($query); | ||
$this->assertArrayNotHasKey('errors', $data, print_r($data, true)); | ||
error_log(print_r($data, true)); | ||
|
||
$expected = [ | ||
'title' => 'Preview post', | ||
'translations' => [ | ||
[ | ||
'title' => 'Finnish post version', | ||
], | ||
], | ||
]; | ||
|
||
$this->assertEquals($expected, $data['data']['post']); | ||
} | ||
} |