Skip to content

Commit 80e5d7a

Browse files
committed
Add case four to FieldtypeOptions test.
1 parent 2a18e4b commit 80e5d7a

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

src/Field/Page/Fieldtype/FieldtypeOptions.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,32 @@
66
use Youshido\GraphQL\Type\ListType\ListType;
77
use ProcessWire\GraphQL\Field\Page\Fieldtype\AbstractFieldtype;
88
use ProcessWire\GraphQL\Type\Object\SelectableOptionType;
9+
use ProcessWire\GraphQL\Type\Enum\SelectableOptionEnumType;
910
use ProcessWire\InputfieldSelectMultiple;
1011

1112
class FieldtypeOptions extends AbstractFieldtype {
1213

13-
public function getDefaultType()
14+
public function isMultiple()
1415
{
15-
$inputfieldClassName = 'ProcessWire\\' . $this->field->inputfieldClass;
16+
$inputfieldClassName = 'ProcessWire\\' . $this->field->inputfieldClass;
1617
$inputfieldClassInstance = new $inputfieldClassName();
17-
if ($inputfieldClassInstance instanceof InputfieldSelectMultiple) {
18+
return $inputfieldClassInstance instanceof InputfieldSelectMultiple;
19+
}
20+
21+
public function getDefaultType()
22+
{
23+
if ($this->isMultiple()) {
1824
return new ListType(new SelectableOptionType());
1925
}
2026
return new SelectableOptionType();
2127
}
2228

29+
public function getInputfieldType($type = null)
30+
{
31+
if ($this->isMultiple()) {
32+
return new ListType(new SelectableOptionEnumType($this->field));
33+
}
34+
return new SelectableOptionEnumType($this->field);
35+
}
36+
2337
}

src/Type/Enum/SelectableOptionEnumType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public function getValues()
1818
$options = [];
1919
foreach ($this->field->type->getOptions($this->field) as $option) {
2020
$options[] = [
21-
'value' => $option->id,
22-
'name' => $option->value | $option->title,
21+
'value' => $option->value,
22+
'name' => $option->title,
2323
];
2424
}
2525
return $options;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* You can pass a plain string for options field that
5+
* stores single option
6+
*/
7+
8+
namespace ProcessWire\GraphQL\Test\Field\Page\Fieldtype\FieldtypeOptions;
9+
10+
use \ProcessWire\GraphQL\Utils;
11+
use \ProcessWire\GraphQL\Test\GraphQLTestCase;
12+
use \ProcessWire\GraphQL\Test\Field\Page\Traits\AccessTrait;
13+
use ProcessWire\NullPage;
14+
15+
class FieldtypeOptionsCaseFourTest extends GraphQLTestCase {
16+
17+
const accessRules = [
18+
'legalTemplates' => ['architects', 'architect'],
19+
'legalFields' => ['options', 'title'],
20+
];
21+
22+
use AccessTrait;
23+
24+
public function testValue()
25+
{
26+
$name = "new-architect";
27+
$title = "New Architect";
28+
$option = ['Mon', 'Thu', 'Sat'];
29+
$parent = "4111";
30+
$query = 'mutation createPage ($page: ArchitectCreateInputType!) {
31+
createArchitect (page: $page) {
32+
name
33+
id
34+
title
35+
options {
36+
title
37+
value
38+
id
39+
}
40+
}
41+
}';
42+
$variables = [
43+
"page" => [
44+
"parent" => $parent,
45+
"name" => $name,
46+
"title" => $title,
47+
"options" => $option,
48+
]
49+
];
50+
$res = $this->execute($query, json_encode($variables));
51+
$newArchitect = Utils::pages()->get("template=architect, name=$name");
52+
$this->assertTrue(!$newArchitect instanceof NullPage, 'New Page is created.');
53+
$this->assertEquals($name, $newArchitect->name, 'New Page has correct name.');
54+
$this->assertEquals($title, $newArchitect->title, 'New Page has correct title.');
55+
$this->assertEquals('Mon', $newArchitect->options->eq(0)->title, 'New Page has correct option title at 0.');
56+
$this->assertEquals('Thursday', $newArchitect->options->eq(1)->value, 'New Page has correct option value at 1.');
57+
$this->assertEquals('6', $newArchitect->options->eq(2)->id, 'New Page has correct option id at 2.');
58+
}
59+
60+
}

test/Field/Page/Fieldtype/FieldtypeOptions/CaseThreeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testValue()
5252
$this->assertTrue(!$newCity instanceof NullPage, 'New Page is created.');
5353
$this->assertEquals($name, $newCity->name, 'New Page has correct name.');
5454
$this->assertEquals($title, $newCity->title, 'New Page has correct title.');
55-
$this->assertEquals($option, $newCity->options_single->value, 'New Page has correct option value.');
55+
$this->assertEquals($option, $newCity->options_single->title, 'New Page has correct option title.');
5656
}
5757

5858
}

0 commit comments

Comments
 (0)