Skip to content

Commit

Permalink
Merge pull request #339 from zendesk/samgavinio/samples-for-helpcente…
Browse files Browse the repository at this point in the history
…r-section

Add support for creating sections and for listing sections for a category
  • Loading branch information
samgavinio authored Jul 14, 2017
2 parents 6a0857b + cb9e15b commit e5e398f
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 18 deletions.
32 changes: 32 additions & 0 deletions samples/helpcenter/createSections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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 Section
$categoryId = 1;
$section = $client->helpCenter->categories($categoryId)->sections()->create([
'position' => 1,
'locale' => 'en-us',
'name' => 'Super Hero Tricks',
'description' => 'This section contains a collection of super hero tricks',
]);
echo "<pre>";
print_r($section);
echo "</pre>";
} catch (\Zendesk\API\Exceptions\ApiResponseException $e) {
echo $e->getMessage().'</br>';
}
45 changes: 45 additions & 0 deletions samples/helpcenter/findSections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 {
// Find all helpcenter sections
$sections = $client->helpCenter->sections()->findAll();
echo "<pre>";
print_r($sections);
echo "</pre>";

// Find all helpcenter sections with a specific locale
$sections = $client->helpCenter->sections()->setLocale('en-us')->findAll();
echo "<pre>";
print_r($sections);
echo "</pre>";

// Find all helpcenter sections within a specific category
$categoryId = 204009948;
$sections = $client->helpCenter->categories($categoryId)->sections()->findAll();
echo "<pre>";
print_r($sections);
echo "</pre>";

// Find all helpcenter sections with a specific locale in a category
$sections = $client->helpCenter->categories($categoryId)->sections()->setLocale('en-us')->findAll();
echo "<pre>";
print_r($sections);
echo "</pre>";
} catch (\Zendesk\API\Exceptions\ApiResponseException $e) {
echo $e->getMessage().'</br>';
}
13 changes: 13 additions & 0 deletions src/Zendesk/API/Resources/HelpCenter/Categories.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 Categories
* https://developer.zendesk.com/rest_api/docs/help_center/categories
* @method Sections sections()
*/
class Categories extends ResourceAbstract
{
use InstantiatorTrait;
use Defaults;
use Locales;

Expand All @@ -19,6 +22,16 @@ class Categories extends ResourceAbstract
*/
protected $objectName = 'category';

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

/**
* @inheritdoc
*/
Expand Down
48 changes: 46 additions & 2 deletions src/Zendesk/API/Resources/HelpCenter/Sections.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
class Sections extends ResourceAbstract
{
use Defaults;
use Locales;
use Locales {
getRoute as protected localesGetRoute;
}

/**
* {@inheritdoc}
Expand All @@ -24,6 +26,48 @@ class Sections extends ResourceAbstract
*/
protected function setUpRoutes()
{
$this->setRoute('updateSourceLocale', "{$this->resourceName}/{sectionId}/source_locale.json");
$this->setRoutes([
'updateSourceLocale' => "{$this->resourceName}/{sectionId}/source_locale.json",
'create' => "{$this->prefix}categories/{category_id}/sections.json",
]);
}

/**
* Returns a route and replaces tokenized parts of the string with
* the passed params
*
* @param string $name
* @param array $params
*
* @return mixed The default routes, or if $name is set to `findAll`, any of the following formats
* based on the parent chain
* GET /api/v2/helpcenter/sections.json
* GET /api/v2/helpcenter/categories/{category_id}/sections.json
*
* @throws \Exception
*/
public function getRoute($name, array $params = [])
{
$lastChained = $this->getLatestChainedParameter();
$params = $this->addChainedParametersToParams($params, [
'category_id' => Categories::class
]);

if (empty($lastChained) || $name !== 'findAll') {
return $this->localesGetRoute($name, $params);
} else {
$chainedResourceName = array_keys($lastChained)[0];
$id = $lastChained[$chainedResourceName];
if ($chainedResourceName === Categories::class) {
$locales = $this->getLocale();
if ($locales) {
return "{$this->prefix}{$locales}/categories/$id/sections.json";
} else {
return "{$this->prefix}categories/$id/sections.json";
}
} else {
return $this->localesGetRoute($name, $params);
}
}
}
}
68 changes: 52 additions & 16 deletions tests/Zendesk/API/UnitTests/HelpCenter/SectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Zendesk\API\UnitTests\HelpCenter;

use Faker\Factory;
use Zendesk\API\Resources\HelpCenter\Sections;
use Zendesk\API\UnitTests\BasicTest;

Expand All @@ -12,41 +13,55 @@ class SectionsTest extends BasicTest
*/
public function testRoutesWithLocale()
{
$faker = Factory::create();
$locale = $faker->locale;

$sectionsResource = $this->client->helpCenter->sections();
$sectionsResource->setLocale('en-US');
$sectionsResource->setLocale($locale);

$this->assertEndpointCalled(function () use ($sectionsResource) {
$sectionsResource->findAll();
}, 'help_center/en-US/sections.json');
}, "help_center/{$locale}/sections.json");

$this->assertEndpointCalled(function () use ($sectionsResource) {
$sectionsResource->find(1);
}, 'help_center/en-US/sections/1.json');
$categoryId = $faker->numberBetween(1);
$this->assertEndpointCalled(function () use ($categoryId, $locale) {
$resource = $this->client->helpCenter->categories($categoryId)->sections();
$resource->setLocale($locale)->findAll();
}, "help_center/{$locale}/categories/{$categoryId}/sections.json");

$sectionId = $faker->numberBetween(1);
$this->assertEndpointCalled(function () use ($sectionsResource, $sectionId) {
$sectionsResource->find($sectionId);
}, "help_center/{$locale}/sections/{$sectionId}.json");

$postFields = [
'position' => 2,
'locale' => 'en-us',
'name' => 'Super Hero Tricks',
'description' => 'This section contains a collection of super hero tricks',
'position' => $faker->numberBetween(1),
'locale' => $locale,
'name' => $faker->sentence,
'description' => $faker->sentence,
];

$this->assertEndpointCalled(function () use ($sectionsResource, $postFields) {
$sectionsResource->create($postFields);
}, 'help_center/en-US/sections.json', 'POST', ['postFields' => ['section' => $postFields]]);

$this->assertEndpointCalled(function () use ($sectionsResource, $postFields) {
$sectionsResource->update(1, $postFields);
}, 'help_center/en-US/sections/1.json', 'PUT', ['postFields' => ['section' => $postFields]]);
$this->assertEndpointCalled(function () use ($sectionsResource, $postFields, $sectionId) {
$sectionsResource->update($sectionId, $postFields);
}, "help_center/{$locale}/sections/{$sectionId}.json", 'PUT', [
'postFields' => ['section' => $postFields]
]);
}

/**
* Tests if the route can be generated
*/
public function testRouteWithoutLocale()
{
$faker = Factory::create();
$this->assertEndpointCalled(function () {
$this->client->helpCenter->sections()->findAll();
}, 'help_center/sections.json');

$categoryId = $faker->numberBetween(1);
$this->assertEndpointCalled(function () use ($categoryId) {
$this->client->helpCenter->categories($categoryId)->sections()->findAll();
}, "help_center/categories/{$categoryId}/sections.json");
}

/**
Expand All @@ -68,4 +83,25 @@ public function testUpdateSectionSourceLocaleNoId()
$this->client->helpCenter->sections(1)->updateSourceLocale(null, 'fr');
}, 'help_center/sections/1/source_locale.json', 'PUT', ['postFields' => ['section_locale' => 'fr']]);
}

/**
* Tests if the Create section endpoint can be called and passed the correct params
*/
public function testCanCreateSection()
{
$faker = Factory::create();
$params = [
'position' => $faker->numberBetween(1),
'name' => $faker->sentence,
'description' => $faker->sentence,
'locale' => $faker->locale,
];
$categoryId = $faker->numberBetween(1);

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

0 comments on commit e5e398f

Please sign in to comment.