Using an IUrlSegmentProvider you can modify the url segment that Umbraco generates for any piece of content based on whatever conditions you like.
For example let’s have a look at the Umbraco outbound request pipeline documentation:
(Notice that I'm injecting the DefaultUrlSegmentProvider in the constructor instead of creating a new instance as the documentation example since it's a bit more test-friendly.)
In this example the productSku will be included in url segments for content of type productPage, so for instance a product called Swibble with sku 123xyz would get a url segment of swibble-123xyz.
Testing all the (Umbraco) things:
A personal goal I have for my UmbracoUnitTesting project is to create test samples for as many different Umbraco components as possible and that they should follow that components documentation as close as possible to ensure that getting started with Unit Testing is as easy as getting started with Umbraco itself.
Please note: I had to make a small adjustment in the RegisterCustomSegmentProviderComposer in the documentation since the DefaultUrlSegmentProvider was not registered in the IoC container out of the box. You can see the change here.
Don’t test Umbraco:
One of the slides for my presentation at the Umbraco Leeds Meetup about “Getting started with Unit Testing in Umbraco“ was about not testing the underlying behavior of Umbraco in your Unit Tests (that would be an Integration test), and I think this test is a great example of that.
I’m not testing if Umbraco can resolve a default url segment or not: I trust that it can. And I'm not testing if Umbraco can get the value, from a cached content item, from a property called productSku: again I trust that it can.
I’m testing my interaction with Umbraco’s default url segment provider and securing the behavior of my own implementation.
"Trust that Umbraco has done its job and write tests to verify that you've done yours."
Hope this can be helpful to someone in the future.
Cheers friends! ❤️