Inheritance: extends Illuminate\Database\Eloquent\Model
 public function setUp()
 {
     parent::setUp();
     $this->user = factory(User::class)->create();
     $this->beatmapset = factory(Beatmapset::class)->create(['user_id' => $this->user->user_id]);
     $this->beatmap = $this->beatmapset->beatmaps()->save(factory(Beatmap::class)->make(['user_id' => $this->user->user_id]));
     $this->beatmapsetDiscussion = BeatmapsetDiscussion::create(['beatmapset_id' => $this->beatmapset->beatmapset_id]);
     $this->beatmapDiscussion = BeatmapDiscussion::create(['beatmapset_discussion_id' => $this->beatmapsetDiscussion->id, 'timestamp' => 0, 'message_type' => 'praise', 'beatmap_id' => $this->beatmap->beatmap_id, 'user_id' => $this->user->user_id]);
 }
 public function setUp()
 {
     parent::setUp();
     $this->user = factory(User::class)->create();
     $this->beatmapset = factory(Beatmapset::class)->create();
     $this->beatmap = $this->beatmapset->beatmaps()->save(factory(Beatmap::class)->make());
     $this->beatmapsetDiscussion = BeatmapsetDiscussion::create(['beatmapset_id' => $this->beatmap->beatmapset_id]);
     $this->beatmapDiscussion = factory(BeatmapDiscussion::class, 'timeline')->create(['beatmapset_discussion_id' => $this->beatmapsetDiscussion->id, 'beatmap_id' => $this->beatmap->beatmap_id, 'user_id' => $this->user->user_id]);
     $this->otherBeatmapset = factory(Beatmapset::class)->create();
     $this->otherBeatmap = $this->otherBeatmapset->beatmaps()->save(factory(Beatmap::class)->make());
 }
 public function store()
 {
     $discussion = BeatmapDiscussion::findOrNew(Request::input('beatmap_discussion_id'));
     $isNewDiscussion = $discussion->id === null;
     if ($isNewDiscussion) {
         $beatmapsetDiscussion = BeatmapsetDiscussion::where('beatmapset_id', Request::input('beatmapset_id'))->firstOrFail();
         $discussion->beatmapset_discussion_id = $beatmapsetDiscussion->id;
     }
     $posts = [new BeatmapDiscussionPost($this->postParams($discussion))];
     $previousDiscussionResolved = $discussion->resolved;
     $discussion->fill($this->discussionParams($isNewDiscussion));
     priv_check('BeatmapDiscussionPost', $discussion)->ensureCan();
     if ($discussion->resolved === true) {
         priv_check('BeatmapDiscussionResolve', $discussion)->ensureCan();
     }
     if (!$isNewDiscussion && $discussion->resolved !== $previousDiscussionResolved) {
         $posts[] = BeatmapDiscussionPost::generateLogResolveChange(Auth::user(), $discussion->resolved);
     }
     try {
         $saved = DB::transaction(function () use($posts, $discussion) {
             if ($discussion->save() === false) {
                 throw new Exception('failed');
             }
             foreach ($posts as $post) {
                 // done here since discussion may or may not previously exist
                 $post->beatmap_discussion_id = $discussion->id;
                 if ($post->save() === false) {
                     throw new Exception('failed');
                 }
             }
             return true;
         });
     } catch (Exception $_e) {
         $saved = false;
     }
     $postIds = array_pluck($posts, 'id');
     if ($saved === true) {
         return ['beatmapset_discussion' => $posts[0]->beatmapsetDiscussion->defaultJson(Auth::user()), 'beatmap_discussion_post_ids' => $postIds, 'beatmap_discussion_id' => $discussion->id];
     } else {
         return error_popup(trans('beatmaps.discussion-posts.store.error'));
     }
 }
Example #4
0
 public function testIsValid()
 {
     $beatmapset = factory(Beatmapset::class)->create();
     $beatmap = $beatmapset->beatmaps()->save(factory(Beatmap::class)->make());
     $otherBeatmapset = factory(Beatmapset::class)->create();
     $otherBeatmap = $otherBeatmapset->beatmaps()->save(factory(Beatmap::class)->make());
     $beatmapsetDiscussion = BeatmapsetDiscussion::create(['beatmapset_id' => $beatmap->beatmapset_id]);
     $invalidTimestamp = $beatmap->total_length * 1000 + 1;
     // blank everything is fine
     $discussion = $this->newDiscussion($beatmapsetDiscussion);
     $this->assertTrue($discussion->isValid());
     // complete data is fine as well
     $discussion = $this->newDiscussion($beatmapsetDiscussion);
     $discussion->fill(['timestamp' => 0, 'message_type' => 'praise', 'beatmap_id' => $beatmap->beatmap_id]);
     $this->assertTrue($discussion->isValid());
     // just timestamp is not valid
     $discussion = $this->newDiscussion($beatmapsetDiscussion);
     $discussion->fill(['timestamp' => 0]);
     $this->assertFalse($discussion->isValid());
     // nor is just beatmap_id
     $discussion = $this->newDiscussion($beatmapsetDiscussion);
     $discussion->fill(['beatmap_id' => $beatmap->beatmap_id]);
     $this->assertFalse($discussion->isValid());
     // nor is just message_type
     $discussion = $this->newDiscussion($beatmapsetDiscussion);
     $discussion->fill(['message_type' => 'praise']);
     $this->assertFalse($discussion->isValid());
     // nor is wrong beatmap_id
     $discussion = $this->newDiscussion($beatmapsetDiscussion);
     $discussion->fill(['timestamp' => 0, 'message_type' => 'praise', 'beatmap_id' => $otherBeatmap->beatmap_id]);
     $this->assertFalse($discussion->isValid());
     // nor is wrong timestamp
     $discussion = $this->newDiscussion($beatmapsetDiscussion);
     $discussion->fill(['timestamp' => $invalidTimestamp, 'message_type' => 'praise', 'beatmap_id' => $beatmap->beatmap_id]);
     $this->assertFalse($discussion->isValid());
 }