Skip to content

Commit

Permalink
Merge pull request zendesk#372 from abackstrom/helpcenter-article-cre…
Browse files Browse the repository at this point in the history
…ation

Implement HelpCenter Article creation
  • Loading branch information
samgavinio authored Jan 26, 2018
2 parents 4a5e705 + 66f324c commit fc7b5be
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
31 changes: 31 additions & 0 deletions samples/helpcenter/createArticles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

include('../../vendor/autoload.php');

use Zendesk\API\HttpClient as ZendeskAPI;

/**
* Replace the following with your own.
*/

$subdomain = "subdomain";
$username = "email@example.com";
$token = "6wiIBWbGkBMo1mRDMuVwkw1EPsNkeUj95PIz2akv";

$client = new ZendeskAPI($subdomain);
$client->setAuth('basic', ['username' => $username, 'token' => $token]);

try {
// Create a new HelpCenter Article
$sectionId = 1;
$article = $client->helpCenter->sections($sectionId)->articles()->create([
'locale' => 'en-us',
'title' => 'Smartest Fish in the World',
]);
echo "<pre>";
print_r($article);
echo "</pre>";
} catch (\Zendesk\API\Exceptions\ApiResponseException $e) {
echo $e->getMessage().'</br>';
}

17 changes: 16 additions & 1 deletion src/Zendesk/API/Resources/HelpCenter/Articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
class Articles extends ResourceAbstract
{
use Defaults;
use Locales;
use Locales {
getRoute as protected localesGetRoute;
}
use Search;

/**
Expand All @@ -30,6 +32,7 @@ protected function setupRoutes()
parent::setUpRoutes();
$this->setRoutes([
'bulkAttach' => "$this->resourceName/{articleId}/bulk_attachments.json",
'create' => "{$this->prefix}sections/{section_id}/articles.json",
'updateSourceLocale' => "$this->resourceName/{articleId}/source_locale.json",
]);
}
Expand Down Expand Up @@ -60,4 +63,16 @@ public function bulkAttach($articleId, array $params, $routeKey = __FUNCTION__)
['attachment_ids' => $params]
);
}

/**
* {@inheritdoc}
*/
public function getRoute($name, array $params = [])
{
$params = $this->addChainedParametersToParams($params, [
'section_id' => Sections::class,
]);

return $this->localesGetRoute($name, $params);
}
}
13 changes: 13 additions & 0 deletions src/Zendesk/API/Resources/HelpCenter/Sections.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use Zendesk\API\Traits\Resource\Defaults;
use Zendesk\API\Traits\Resource\Locales;
use Zendesk\API\Traits\Utility\InstantiatorTrait;

/**
* Class Sections
* https://developer.zendesk.com/rest_api/docs/help_center/categories
*/
class Sections extends ResourceAbstract
{
use InstantiatorTrait;

use Defaults;
use Locales {
getRoute as protected localesGetRoute;
Expand All @@ -32,6 +35,16 @@ protected function setUpRoutes()
]);
}

/**
* {@inheritdoc}
*/
public static function getValidSubResources()
{
return [
'articles' => Articles::class,
];
}

/**
* Returns a route and replaces tokenized parts of the string with
* the passed params
Expand Down
16 changes: 16 additions & 0 deletions tests/Zendesk/API/UnitTests/HelpCenter/ArticlesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ public function testSearch()
$this->client->helpCenter->articles()->search($params);
}, 'help_center/articles/search.json', 'GET', ['queryParams' => $params]);
}

/**
* Test if Create article endpoint is called with correct params.
*/
public function testCreate()
{
$faker = Factory::create();
$sectionId = $faker->numberBetween(1);
$params = ['title' => $faker->word, 'locale' => 'en'];

$this->assertEndpointCalled(function () use ($params, $sectionId) {
$this->client->helpCenter->sections($sectionId)->articles()->create($params);
}, "help_center/sections/{$sectionId}/articles.json", 'POST', [
'postFields' => ['article' => $params],
]);
}
}

0 comments on commit fc7b5be

Please sign in to comment.