From 71123d5a31a03c3d4c9678500bcb3e9e8b950f81 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 19 May 2017 10:05:39 +0100 Subject: [PATCH 1/7] Search the syndication targets array properly --- app/Http/Controllers/MicropubController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index 2aba67cc..4d3f9f8e 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -98,7 +98,7 @@ class MicropubController extends Controller $data['syndicate'] = []; $targets = array_pluck(config('syndication.targets'), 'uid', 'service.name'); if (is_string($request->input('mp-syndicate-to'))) { - $service = array_search($request->input('mp-syndicate-to')); + $service = array_search($request->input('mp-syndicate-to'), $targets); if ($service == 'Twitter') { $data['syndicate'][] = 'twitter'; } From 9d9e3577bcafd7a75112f274dbd357d309d71423 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 19 May 2017 10:07:43 +0100 Subject: [PATCH 2/7] Form the micropub JSON correctly --- app/Http/Controllers/MicropubClientController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/MicropubClientController.php b/app/Http/Controllers/MicropubClientController.php index 9d6f8d6d..91d4eb5a 100644 --- a/app/Http/Controllers/MicropubClientController.php +++ b/app/Http/Controllers/MicropubClientController.php @@ -206,16 +206,16 @@ class MicropubClientController extends Controller $json['properties'] = ['content' => [$request->input('content')]]; if ($request->input('in-reply-to') != '') { - $json['properties']['in-reply-to'] = [$request->input('in-reply-to')]; + $json['properties']['in-reply-to'][] = $request->input('in-reply-to'); } if ($request->input('mp-syndicate-to')) { foreach ($request->input('mp-syndicate-to') as $syn) { - $json['properties']['mp-syndicate-to'] = [$syn]; + $json['properties']['mp-syndicate-to'][] = $syn; } } if ($request->input('location')) { if ($request->input('location') !== 'no-location') { - $json['properties']['location'] = [$request->input('location')]; + $json['properties']['location'][] = $request->input('location'); } } if ($request->input('media')) { From cdf265fcc1950ddb7c4d7522153b2ca439ae8fc7 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 19 May 2017 11:30:28 +0100 Subject: [PATCH 3/7] Add a test for generated JSON --- .../Feature/MicropubClientControllerTest.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/Feature/MicropubClientControllerTest.php diff --git a/tests/Feature/MicropubClientControllerTest.php b/tests/Feature/MicropubClientControllerTest.php new file mode 100644 index 00000000..19d0b407 --- /dev/null +++ b/tests/Feature/MicropubClientControllerTest.php @@ -0,0 +1,51 @@ + 'http://example.org/a'], 'Created'), + ]); + + $stack = HandlerStack::create($mock); + $stack->push($history); + $guzzleClient = new GuzzleClient(['handler' => $stack]); + + $this->app->instance(GuzzleClient::class, $guzzleClient); + + $response = $this->post( + '/micropub', + [ + 'content' => 'Hello Fred', + 'in-reply-to' => 'https://fredbloggs.com/note/abc', + 'mp-syndicate-to' => ['https://twitter.com/jonnybarnes', 'https://facebook.com/jonnybarnes'], + ] + ); + + $expected = '{"type":["h-entry"],"properties":{"content":["Hello Fred"],"in-reply-to":["https:\/\/fredbloggs.com\/note\/abc"],"mp-syndicate-to":["https:\/\/twitter.com\/jonnybarnes","https:\/\/facebook.com\/jonnybarnes"]}}'; + foreach ($container as $transaction) { + $this->assertEquals($expected, $transaction['request']->getBody()->getContents()); + } + } +} From 2d8678b03757acff3b2ac61c7494c5d754404b5b Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 19 May 2017 13:16:30 +0100 Subject: [PATCH 4/7] Add some tests for syndication jobs --- tests/Feature/NoteServiceTest.php | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/Feature/NoteServiceTest.php diff --git a/tests/Feature/NoteServiceTest.php b/tests/Feature/NoteServiceTest.php new file mode 100644 index 00000000..a7b44db1 --- /dev/null +++ b/tests/Feature/NoteServiceTest.php @@ -0,0 +1,60 @@ +createNote([ + 'content' => 'Hello Fred', + 'in-reply-to' => 'https://fredbloggs.com/note/abc', + 'syndicate' => ['twitter'], + ]); + + Queue::assertPushed(SyndicateToTwitter::class); + } + + public function test_syndicate_to_facebook_job_is_sent() + { + Queue::fake(); + + $noteService = new NoteService(); + $note = $noteService->createNote([ + 'content' => 'Hello Fred', + 'in-reply-to' => 'https://fredbloggs.com/note/abc', + 'syndicate' => ['facebook'], + ]); + + Queue::assertPushed(SyndicateToFacebook::class); + } + + public function test_syndicate_to_target_jobs_are_sent() + { + Queue::fake(); + + $noteService = new NoteService(); + $note = $noteService->createNote([ + 'content' => 'Hello Fred', + 'in-reply-to' => 'https://fredbloggs.com/note/abc', + 'syndicate' => ['twitter', 'facebook'], + ]); + + Queue::assertPushed(SyndicateToTwitter::class); + Queue::assertPushed(SyndicateToFacebook::class); + } +} From 67782e30e85455329010e87f08bc0d4425649902 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 19 May 2017 13:17:27 +0100 Subject: [PATCH 5/7] Add check that photos key exists before trying to do something with it --- app/Services/NoteService.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/Services/NoteService.php b/app/Services/NoteService.php index 09dbb1c9..83cc7f2d 100644 --- a/app/Services/NoteService.php +++ b/app/Services/NoteService.php @@ -76,18 +76,20 @@ class NoteService } */ //add support for media uploaded as URLs - foreach ($data['photo'] as $photo) { - // check the media was uploaded to my endpoint, and use path - if (starts_with($photo, config('filesystems.disks.s3.url'))) { - $path = substr($photo, strlen(config('filesystems.disks.s3.url'))); - $media = Media::where('path', ltrim($path, '/'))->firstOrFail(); - } else { - $media = Media::firstOrNew(['path' => $photo]); - // currently assuming this is a photo from Swarm - $media->type = 'image'; - $media->save(); + if (array_key_exists('photo', $data)) { + foreach ($data['photo'] as $photo) { + // check the media was uploaded to my endpoint, and use path + if (starts_with($photo, config('filesystems.disks.s3.url'))) { + $path = substr($photo, strlen(config('filesystems.disks.s3.url'))); + $media = Media::where('path', ltrim($path, '/'))->firstOrFail(); + } else { + $media = Media::firstOrNew(['path' => $photo]); + // currently assuming this is a photo from Swarm + $media->type = 'image'; + $media->save(); + } + $note->media()->save($media); } - $note->media()->save($media); } $note->save(); From e21a04a34261126278d318aef04e4a6f9dfc08e2 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 19 May 2017 13:18:34 +0100 Subject: [PATCH 6/7] Update changelog --- changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/changelog.md b/changelog.md index 6a0ddbcf..069848d6 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## Version {next} + - improve test suite + ## Version 0.5.4 (2017-05-18) - Fix issues with using the indieauth client From 982c36ce64cf34f48de0089da2d48f2485150962 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 19 May 2017 13:34:47 +0100 Subject: [PATCH 7/7] Bump version number to 0.5.5 --- changelog.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 069848d6..f2f300c1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,8 @@ # Changelog -## Version {next} +## Version 0.5.5 (2017-05-19) - improve test suite + - Syndication should now work ## Version 0.5.4 (2017-05-18) - Fix issues with using the indieauth client