From fb8d2649e17c94458db247925b796f5ae2141b0d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 1 Sep 2021 22:01:42 +0200 Subject: Fix saving and fetching roles from Concerts table. --- includes/concert.php | 42 ++++++++++++++++++++---------------------- tests/ConcertTest.php | 23 ++++------------------- 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/includes/concert.php b/includes/concert.php index db8d68a..53cd00e 100644 --- a/includes/concert.php +++ b/includes/concert.php @@ -76,7 +76,12 @@ if ( !class_exists('GiglogAdmin_Concert') ) { $results = $wpdb->get_results($query); - return $results ? new GiglogAdmin_Concert($results[0]) : NULL; + if ( !$results ) { + $wpdb->print_error( __METHOD__ ); + return null; + } + + return new GiglogAdmin_Concert($results[0]); } public static function create(string $name, $venue, string $date, string $ticketlink, string $eventlink): ?self @@ -202,34 +207,27 @@ if ( !class_exists('GiglogAdmin_Concert') ) { { global $wpdb; - if ( $this->id !== NULL ) { - $res = $wpdb->update('wpg_concerts', array( - 'id' => $this->id, - 'wpgconcert_name' => $this->cname, - 'venue' => $this->venue->id(), - 'wpgconcert_date' => $this->cdate, - 'wpgconcert_tickets' => $this->tickets, - 'wpgconcert_event' => $this->eventlink, - 'wpgconcert_status' => $this->status, - ), - array( 'id' => $this->id ) ); + $columns = [ + 'wpgconcert_name' => $this->cname, + 'venue' => $this->venue->id(), + 'wpgconcert_date' => $this->cdate, + 'wpgconcert_tickets' => $this->tickets, + 'wpgconcert_event' => $this->eventlink, + 'wpgconcert_status' => $this->status, + 'wpgconcert_roles' => wp_json_encode( $this->roles ), + ]; + if ( $this->id !== NULL ) { + $res = $wpdb->update( 'wpg_concerts', $columns, [ 'id' => $this->id ] ); } else { - $res = $wpdb->insert('wpg_concerts', array( - 'wpgconcert_name' => $this->cname, - 'venue' => $this->venue->id(), - 'wpgconcert_date' => $this->cdate, - 'wpgconcert_tickets' => $this->tickets, - 'wpgconcert_event' => $this->eventlink, - 'wpgconcert_status' => $this->status, - )); + $res = $wpdb->insert('wpg_concerts', $columns); } if ( $res === false ) { $wpdb->print_error( __METHOD__ ); } - else { + elseif ( $this->id === NULL ) { $this->id = $wpdb->insert_id; } } @@ -275,7 +273,7 @@ if ( !class_exists('GiglogAdmin_Concert') ) { return $this->roles; } - public function assign_role( int $role, string $username ) : void + public function assign_role( string $role, string $username ) : void { $this->roles[$role] = $username; } diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php index 0bf345c..5e6ebde 100644 --- a/tests/ConcertTest.php +++ b/tests/ConcertTest.php @@ -135,27 +135,12 @@ final class ConcertTest extends WP_UnitTestCase public function testAssignConcertRoles() : void { - $venue = GiglogAdmin_Venue::create("a venue"); - $today = date("Y-m-d"); - - $gig = GiglogAdmin_Concert::create( - "a concert123", - $venue->id(), - $today, - "https://example.com/tickets/42", - "https://example.com/events/93"); - - $gig->assign_role( GiglogAdmin_Roles::PHOTO1, 'user1' ); - $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $gig->roles() ); - + $gig = GiglogAdmin_Concert::get(self::$concerts[0]->id()); + $gig->assign_role( 'photo1' , 'user1' ); $gig->save(); - var_dump($gig); - - $fetched_gig = GiglogAdmin_Concert::get( $gig->id() ); - global $wpdb; - $wpdb->print_error(); - $this->assertEquals( [ GiglogAdmin_Roles::PHOTO1 => 'user1' ], $fetched_gig->roles() ); + $fetched_gig = GiglogAdmin_Concert::get( self::$concerts[0]->id() ); + $this->assertEquals( [ 'photo1' => 'user1' ], $fetched_gig->roles() ); } public function testOnlyFetchConcertsFromGivenCity() : void -- cgit v1.2.3