Skip to content

Commit

Permalink
Merge pull request #377 from Art4/wiki-attachments
Browse files Browse the repository at this point in the history
Test handling attachments with wiki pages
  • Loading branch information
Art4 authored Feb 8, 2024
2 parents 6083781 + 789efb3 commit 6a4e464
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Parameter types for IDs were fixed in API for attachments, groups, issues, project, users and versions.
- Wiki pages with special characters are now handled correctly

## [v2.5.0](https://github.com/kbsali/php-redmine-api/compare/v2.4.0...v2.5.0) - 2024-02-05

Expand Down
3 changes: 2 additions & 1 deletion src/Redmine/Api/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Redmine\Exception\UnexpectedResponseException;
use Redmine\Serializer\PathSerializer;
use Redmine\Serializer\XmlSerializer;
use SimpleXMLElement;

/**
* Listing projects, creating, editing.
Expand Down Expand Up @@ -146,7 +147,7 @@ public function show($id, array $params = [])
*
* @throws MissingParameterException
*
* @return string|false
* @return string|SimpleXMLElement|false
*/
public function create(array $params = [])
{
Expand Down
10 changes: 6 additions & 4 deletions src/Redmine/Api/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public function show($project, $page, $version = null)
];

if (null === $version) {
$path = '/projects/' . $project . '/wiki/' . $page . '.json';
$path = '/projects/' . $project . '/wiki/' . urlencode($page) . '.json';
} else {
$path = '/projects/' . $project . '/wiki/' . $page . '/' . $version . '.json';
$path = '/projects/' . $project . '/wiki/' . urlencode($page) . '/' . $version . '.json';
}

return $this->get(
Expand All @@ -129,7 +129,7 @@ public function create($project, $page, array $params = [])
$params = $this->sanitizeParams($defaults, $params);

return $this->put(
'/projects/' . $project . '/wiki/' . $page . '.xml',
'/projects/' . $project . '/wiki/' . urlencode($page) . '.xml',
XmlSerializer::createFromArray(['wiki_page' => $params])->getEncoded()
);
}
Expand Down Expand Up @@ -160,6 +160,8 @@ public function update($project, $page, array $params = [])
*/
public function remove($project, $page)
{
return $this->delete('/projects/' . $project . '/wiki/' . $page . '.xml');
return $this->delete(
'/projects/' . $project . '/wiki/' . urlencode($page) . '.xml'
);
}
}
68 changes: 68 additions & 0 deletions tests/End2End/Attachment/AttachmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Redmine\Tests\End2End\Attachment;

use Redmine\Api\Attachment;
use Redmine\Api\Project;
use Redmine\Tests\End2End\ClientTestCase;
use Redmine\Tests\RedmineExtension\RedmineVersion;

class AttachmentTest extends ClientTestCase
{
/**
* @dataProvider provideRedmineVersions
*/
public function testInteractions(RedmineVersion $redmineVersion): void
{
$client = $this->getNativeCurlClient($redmineVersion);

// Create project
/** @var Project */
$projectApi = $client->getApi('project');

$projectIdentifier = 'project-with-wiki';

$xmlData = $projectApi->create(['name' => 'project with wiki', 'identifier' => $projectIdentifier]);

$projectDataJson = json_encode($xmlData);
$projectData = json_decode($projectDataJson, true);

$this->assertIsArray($projectData, $projectDataJson);
$this->assertSame($projectIdentifier, $projectData['identifier'], $projectDataJson);

// Upload file
/** @var Attachment */
$attachmentApi = $client->getApi('attachment');

$jsonData = $attachmentApi->upload(file_get_contents(dirname(__FILE__, 3) . '/Fixtures/testfile_01.txt'), ['filename' => 'testfile.txt']);

$attachmentData = json_decode($jsonData, true);

$this->assertIsArray($attachmentData, $jsonData);
$this->assertArrayHasKey('upload', $attachmentData, $jsonData);
$this->assertSame(
['id', 'token'],
array_keys($attachmentData['upload']),
$jsonData
);

$attachmentToken = $attachmentData['upload']['token'];

$this->assertSame('1.7b962f8af22e26802b87abfa0b07b21dbd03b984ec8d6888dabd3f69cff162f8', $attachmentToken);

// Check attachment
$attachmentData = $attachmentApi->show($attachmentData['upload']['id']);

$jsonData = json_encode($attachmentData);

$this->assertIsArray($attachmentData, $jsonData);
$this->assertArrayHasKey('attachment', $attachmentData, $jsonData);
$this->assertSame(
['id', 'filename', 'filesize', 'content_type', 'description', 'content_url', 'author', 'created_on'],
array_keys($attachmentData['attachment']),
$jsonData
);
}
}
100 changes: 100 additions & 0 deletions tests/End2End/Wiki/WikiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Redmine\Tests\End2End\Wiki;

use Redmine\Api\Attachment;
use Redmine\Api\Project;
use Redmine\Api\Wiki;
use Redmine\Tests\End2End\ClientTestCase;
use Redmine\Tests\RedmineExtension\RedmineVersion;

class WikiTest extends ClientTestCase
{
/**
* @dataProvider provideRedmineVersions
*/
public function testInteractions(RedmineVersion $redmineVersion): void
{
$client = $this->getNativeCurlClient($redmineVersion);

// Create project
/** @var Project */
$projectApi = $client->getApi('project');

$projectIdentifier = 'project-with-wiki';

$xmlData = $projectApi->create(['name' => 'project with wiki', 'identifier' => $projectIdentifier]);

$projectDataJson = json_encode($xmlData);
$projectData = json_decode($projectDataJson, true);

$this->assertIsArray($projectData, $projectDataJson);
$this->assertSame($projectIdentifier, $projectData['identifier'], $projectDataJson);

// Upload file
/** @var Attachment */
$attachmentApi = $client->getApi('attachment');

$jsonData = $attachmentApi->upload(file_get_contents(dirname(__FILE__, 3) . '/Fixtures/testfile_01.txt'), ['filename' => 'testfile.txt']);

$attachmentData = json_decode($jsonData, true);

$this->assertIsArray($attachmentData, $jsonData);
$this->assertArrayHasKey('upload', $attachmentData, $jsonData);
$this->assertSame(
['id', 'token'],
array_keys($attachmentData['upload']),
$jsonData
);

$attachmentToken = $attachmentData['upload']['token'];

$this->assertSame('1.7b962f8af22e26802b87abfa0b07b21dbd03b984ec8d6888dabd3f69cff162f8', $attachmentToken);

// Add attachment to wiki page
/** @var Wiki */
$wikiApi = $client->getApi('wiki');

$xmlData = $wikiApi->create($projectIdentifier, 'Test Page', [
'text' => '# First Wiki page',
'uploads' => [
['token' => $attachmentToken, 'filename' => 'filename.txt', 'content-type' => 'text/plain'],
],
]);

$wikiDataJson = json_encode($xmlData);
$wikiData = json_decode($wikiDataJson, true);

$this->assertIsArray($wikiData, $wikiDataJson);
$this->assertSame(
['title', 'text', 'version', 'author', 'comments', 'created_on', 'updated_on'],
array_keys($wikiData),
$wikiDataJson
);
$this->assertSame('Test+Page', $wikiData['title'], $wikiDataJson);

// Check attachments
$wikiData = $wikiApi->show($projectIdentifier, 'Test Page');

$this->assertIsArray($wikiData, json_encode($wikiData));
$this->assertIsArray($wikiData['wiki_page']['attachments'][0]);
$this->assertSame(
['id', 'filename', 'filesize', 'content_type', 'description', 'content_url', 'author', 'created_on'],
array_keys($wikiData['wiki_page']['attachments'][0])
);

// Update wiki page returns empty string
$returnData = $wikiApi->update($projectIdentifier, 'Test Page', [
'text' => '# First Wiki page with changes',
]);

$this->assertSame('', $returnData, json_encode($returnData));

// Remove wiki page
$returnData = $wikiApi->remove($projectIdentifier, 'Test Page');

$this->assertSame('', $returnData, json_encode($returnData));
}
}
20 changes: 10 additions & 10 deletions tests/Integration/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,45 +487,45 @@ public function testWiki()
{
/** @var \Redmine\Api\Wiki */
$api = MockClient::create()->getApi('wiki');
$res = $api->create('testProject', 'about', [
$res = $api->create('testProject', 'about page', [
'text' => 'asdf',
'comments' => 'asdf',
'version' => 'asdf',
]);
$res = json_decode($res, true);

$this->assertEquals('/projects/testProject/wiki/about.xml', $res['path']);
$this->assertEquals('/projects/testProject/wiki/about+page.xml', $res['path']);
$this->assertEquals('PUT', $res['method']);

$res = $api->update('testProject', 'about', [
$res = $api->update('testProject', 'about page', [
'text' => 'asdf',
'comments' => 'asdf',
'version' => 'asdf',
]);
$res = json_decode($res, true);

$this->assertEquals('/projects/testProject/wiki/about.xml', $res['path']);
$this->assertEquals('/projects/testProject/wiki/about+page.xml', $res['path']);
$this->assertEquals('PUT', $res['method']);

$res = $api->all('testProject');

$this->assertEquals('/projects/testProject/wiki/index.json', $res['path']);
$this->assertEquals('GET', $res['method']);

$res = $api->show('testProject', 'about');
$res = $api->show('testProject', 'about page');

$this->assertEquals('/projects/testProject/wiki/about.json?include=attachments', $res['path']);
$this->assertEquals('/projects/testProject/wiki/about+page.json?include=attachments', $res['path']);
$this->assertEquals('GET', $res['method']);

$res = $api->show('testProject', 'about', 18);
$res = $api->show('testProject', 'about page', 18);

$this->assertEquals('/projects/testProject/wiki/about/18.json?include=attachments', $res['path']);
$this->assertEquals('/projects/testProject/wiki/about+page/18.json?include=attachments', $res['path']);
$this->assertEquals('GET', $res['method']);

$res = $api->remove('testProject', 'about');
$res = $api->remove('testProject', 'about page');
$res = json_decode($res, true);

$this->assertEquals('/projects/testProject/wiki/about.xml', $res['path']);
$this->assertEquals('/projects/testProject/wiki/about+page.xml', $res['path']);
$this->assertEquals('DELETE', $res['method']);
}
}
4 changes: 2 additions & 2 deletions tests/Integration/WikiXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public function testCreateComplex()
{
/** @var \Redmine\Api\Wiki */
$api = MockClient::create()->getApi('wiki');
$res = $api->create('testProject', 'about', [
$res = $api->create('testProject', 'about page', [
'text' => 'asdf',
'comments' => 'asdf',
'version' => 'asdf',
]);
$response = json_decode($res, true);

$this->assertEquals('PUT', $response['method']);
$this->assertEquals('/projects/testProject/wiki/about.xml', $response['path']);
$this->assertEquals('/projects/testProject/wiki/about+page.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
Expand Down
Loading

0 comments on commit 6a4e464

Please sign in to comment.