Skip to content

Commit 7b0d157

Browse files
authored
fix(page): error when updating pages with People properties (#402)
1 parent 4be5828 commit 7b0d157

5 files changed

Lines changed: 69 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## Fixed
10+
- Error when updating pages with People properties.
11+
912
## Build
10-
- Fix failing CI actions due to outdated codecov version.
13+
- Fix failing CI actions due to outdated dependencies.
1114

1215
## [v1.14.0] 2024-04-22
1316

src/Pages/Properties/People.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function toArray(): array
5353

5454
$array["people"] = array_map(
5555
function (User $user): array {
56-
return $user->toArray();
56+
return [ "id" => $user->id ];
5757
},
5858
$this->users,
5959
);

src/Users/User.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ private function __construct(
3030
) {
3131
}
3232

33+
public static function create(string $id): self
34+
{
35+
return new self(
36+
id: $id,
37+
name: null,
38+
avatarUrl: null,
39+
type: null,
40+
person: null,
41+
bot: null,
42+
);
43+
}
44+
3345
/** @psalm-param UserJson $array */
3446
public static function fromArray(array $array): self
3547
{

tests/Integration/DatabasesTest.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Notion\Databases\Database;
1010
use Notion\Databases\DatabaseParent;
1111
use Notion\Databases\Properties\Date;
12+
use Notion\Databases\Properties\People;
1213
use Notion\Databases\Properties\RichTextProperty;
1314
use Notion\Databases\Properties\Select;
1415
use Notion\Databases\Properties\SelectOption;
@@ -21,8 +22,10 @@
2122
use Notion\Pages\Page;
2223
use Notion\Pages\PageParent;
2324
use Notion\Pages\Properties\Date as DateProp;
25+
use Notion\Pages\Properties\People as PeopleProp;
2426
use Notion\Pages\Properties\Select as SelectProp;
2527
use Notion\Search\Query as SearchQuery;
28+
use Notion\Users\User;
2629
use PHPUnit\Framework\TestCase;
2730

2831
class DatabasesTest extends TestCase
@@ -204,13 +207,46 @@ public function test_query_inexistent_database(): void
204207
$client->databases()->query($database, $query);
205208
}
206209

210+
public function test_rename_database_with_people_property(): void
211+
{
212+
$client = Helper::client();
213+
214+
$database = self::moviesDatabase();
215+
$database = $database->changeTitle("New movies database");
216+
$database = $client->databases()->update($database);
217+
218+
$client->databases()->delete($database);
219+
220+
$this->assertSame("New movies database", $database->title[0]->plainText);
221+
}
222+
223+
public function test_rename_database_page_with_people_property(): void
224+
{
225+
$client = Helper::client();
226+
227+
$database = self::moviesDatabase();
228+
229+
$users = Helper::client()->users()->findAll();
230+
$userId = $users[0]->id;
231+
232+
$newPage = self::moviePage($database->id, "Sample movie", "2023-01-01", "Action", $userId);
233+
$newPage = $client->pages()->create($newPage);
234+
235+
$newPage = $newPage->changeTitle("Updated sample movie");
236+
$client->pages()->update($newPage);
237+
238+
$client->databases()->delete($database);
239+
240+
$this->assertSame("Updated sample movie", $newPage->title()?->toString());
241+
}
242+
207243
private static function moviesDatabase(): Database
208244
{
209245
$databaseParent = DatabaseParent::page(Helper::testPageId());
210246

211247
$categories = [
212248
SelectOption::fromName("Action")->changeColor(Color::Orange),
213-
SelectOption::fromname("Comedy")->changeColor(Color::Yellow),
249+
SelectOption::fromName("Comedy")->changeColor(Color::Yellow),
214250
SelectOption::fromName("Drama")->changeColor(Color::Red),
215251
];
216252

@@ -220,16 +256,20 @@ private static function moviesDatabase(): Database
220256
"Movies" => Title::create("Movie"),
221257
"Release date" => Date::create("Release date"),
222258
"Category" => Select::create("Category", $categories),
259+
"People" => People::create("People"),
223260
]);
224261

225262
$database = Helper::client()->databases()->create($database);
226263

264+
$users = Helper::client()->users()->findAll();
265+
$userId = $users[0]->id;
266+
227267
$pages = [
228-
self::moviePage($database->id, "A Clockwork Orange", "1972-12-19", "Drama"),
229-
self::moviePage($database->id, "Dead Poets Society", "1989-06-02", "Drama"),
230-
self::moviePage($database->id, "Batman", "1989-10-26", "Action"),
231-
self::moviePage($database->id, "The Mask", "1994-12-23", "Comedy"),
232-
self::moviePage($database->id, "American Beauty", "1999-09-08", "Drama"),
268+
self::moviePage($database->id, "A Clockwork Orange", "1972-12-19", "Drama", $userId),
269+
self::moviePage($database->id, "Dead Poets Society", "1989-06-02", "Drama", $userId),
270+
self::moviePage($database->id, "Batman", "1989-10-26", "Action", $userId),
271+
self::moviePage($database->id, "The Mask", "1994-12-23", "Comedy", $userId),
272+
self::moviePage($database->id, "American Beauty", "1999-09-08", "Drama", $userId),
233273
];
234274

235275
$client = Helper::client();
@@ -244,13 +284,15 @@ private static function moviePage(
244284
string $databaseId,
245285
string $title,
246286
string $releaseDate,
247-
string $category
287+
string $category,
288+
string $userId
248289
): Page {
249290
$date = new DateTimeImmutable($releaseDate);
250291
return Page::create(PageParent::database($databaseId))
251292
->changeTitle($title)
252293
->addProperty("Release date", DateProp::create($date))
253-
->addProperty("Category", SelectProp::fromname($category));
294+
->addProperty("Category", SelectProp::fromName($category))
295+
->addProperty("People", PeopleProp::create(User::create($userId)));
254296
}
255297

256298
private static function bigDatabase(): Database

tests/Unit/Pages/Properties/PeopleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function test_array_conversion(): void
5757
"id" => "abc",
5858
"type" => "people",
5959
"people" => [
60-
$this->user1()->toArray(),
61-
$this->user2()->toArray(),
60+
[ "id" => $this->user1()->id ],
61+
[ "id" => $this->user2()->id ],
6262
],
6363
];
6464

0 commit comments

Comments
 (0)