Testing duplicate repost prevention0:00
Yep, we have more repost tests. What we need to be sure of is that we prevent duplicate reposts. And it really doesn't matter what the profile is because a profile can repost their own posts. I mean, that's just how it is. But what we want to do is be sure that we can only repost the same post once basically, at least for each profile. So our test is gonna look something like this. First of all, we need a post to work with.
So our test is gonna look something like this. First of all, we need a Post to work with. So we will use the factory to create our post. Then we need a Profile and, technically we could use the same Profile that's created the post, but for the sake of clarity, I think we will just create a separate Profile and then we will repost using that Profile so that, uh, we will have two reposts, we'll call it R1.
and then we will repost using that profile so that uh, we we'll have two reposts, we'll call it R1. And we will repost using the profile and the post, which is going to be the original. Let's change that so that it is simply original. And then we will have a second repost and ideally R1 and R2 will have the same IDs. So that is what we are going to expect that R1 ID is going to be R2 ID. Now of course this is going to fail outright
ID is going to be R two id. Now of course this is going to fail outright because we don't have anything in place to make this work. It is going to create two separate reposts, and it is of course not what we want, but we'll see it fail to begin with. And then we will go from there. Because really there's two things that we need to do, yes, failed, asserting that two is identical to three. Now we essentially want
Adding database uniqueness constraint1:49
failed, asserting that two is identical to three. Now we essentially want to attack this in two different ways. First of all, we want to be sure that we can't have duplicates inside of the database. So at the database level, we need to ensure that there is a unique profile_id with the repost_id. First thing. Second thing is the software has to take that into account, but once we get the database set up,
First thing. Second thing is the software has to take that into account, but once we get the database set up, it's going to be relatively easy. So let's open up the create_hosts migration, because that is where we need to add our unique restriction so that we will call the unique method and we want, its unique based upon the profile_id and the repost_id. And then we need to give this a name, which we can just call it unique_profile_repost.
And then we need to give this a name, which we can just call it uniqueProfilePost. And I think that's, that's gonna be fine. So with that in place, we need to migrate our database. We'll just use php artisan migrate:fresh and then we'll run our test. Again, it's still going to fail, but we should get an error. Now we should see that we can't create another record because of the integrity constraint. Sure enough, there it is. So the database side is fixed.
Updating model to firstOrCreate2:58
because of the integrity constraint. Sure enough, there it is. So the database side is fixed. Now we just need to fix the software side, which we're gonna open up the Post model and we're gonna change our repost methods so that instead of just creating, we'll call firstOrCreate, that should solve that particular problem so that it will fetch the record if it's there. And if it's not, then it will create that. So this should fix our problem so
Testing repost removal3:25
And if it's not, then it will create that. So this should fix our problem so that if we run the test now it should pass. And sure enough, we have seven passing tests, but if we can repost, we should be able to remove a repost. So let's implement that. So we will test that. We remove a repost, and you know, to start with, we're gonna need the exact same things. So we will need the original post
we're gonna need the exact same things. So we will need the original Post and the Profile, except what we'll do is this. We are going to use our PostFactory to repost the original, because ultimately we don't need the repost entity here. We want to create that repost, but we need the Profile more than anything else. So that we will have the original Post, and then we will have the Profile of who made the repost.
So that we will have the original Post, and then we will have the profile of who made the repost. Now, the reason why I'm doing this is because if we already have the repost, then it's pretty easy. We just delete the repost and then we're done. But if we have just the original Post and the profile, well then we will want to do something like this to where we remove the repost and then we would pass in the profile.
to do something like this to where we remove the repost and then we would pass in the profile and the original post that was reposted. And we can say that this is going to give us a boolean value. So if this is true, then it was removed; if false, then of course it wasn't for whatever reason. And so in this case, we expect our original to have no reposts because we deleted it. So we'll say that it has a count of zero.
to have no reposts because we deleted it. So we'll say that to have count of zero. But we also want to be sure that our success variable is true, and that should be all that we need to do. Uh, we probably should make this so that it fails. So we know that it should be zero. So if this passes, then there's definitely something wrong. But of course we need to implement this. Remove repost. So let's go back to our Post model
Implementing removeRepost method5:32
But of course we need to implement this. Remove repost. So let's go back to our Post model and let's write that static function called removeRepost. And we will have the profile as well as the original post, and we'll just call it post. Let's call it original. That way it, it's clear as to what kind of post this is. So this will be the original post so that we will return and we want to get the record where the profile_id.
So this will be the original post so that we will return and we want to get the record where the profile_id is the provided profiles_id. But we also want to wear the post, no, it's not post repost of id. We should rename that something, but oh, well. And this would be the original post id. And then we will simply delete and we will return if that is greater than zero, because the delete method is going to return the number
and we will return if that is greater than zero, because the delete method is going to return the number of records that was deleted. So if we delete one record, then this will return true, otherwise it returns false. And yeah, that should work. So if we run our test, it should fail because this first expectation to have a count of one, it should be zero. And sure enough, it is zero.
of one, it should be zero. And sure enough, it is zero. So let's change that to zero so that we can run our test. Hopefully everything will pass. And sure enough, we have eight passing tests. So now we have all of the functionality of working with reposts, at least all of the functionality that comes to the top of my head. We need to now start focusing on other behaviors such as liking posts,
Next: post liking behavior7:16
We need to now start focusing on other behaviors such as liking Posts, and we will start with that in the next episode.
